001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.auth.camara;
017
018import com.vonage.client.QueryParamsRequest;
019import java.net.URI;
020import java.util.LinkedHashMap;
021import java.util.Map;
022import java.util.Objects;
023
024/**
025 * Represents the request parameters for the intermediate (second) step in an OAuth2 three-legged workflow.
026 */
027public final class TokenRequest implements QueryParamsRequest {
028    private final Map<String, String> params = new LinkedHashMap<>(4);
029
030    /**
031     * Creates a new Back-End token request.
032     *
033     * @param authReqId The auth request ID, as obtained from {@link BackendAuthResponse#getAuthReqId()}.
034     */
035    public TokenRequest(String authReqId) {
036        params.put("grant_type", "urn:openid:params:grant-type:ciba");
037        params.put("auth_req_id", Objects.requireNonNull(authReqId, "Auth request ID is required."));
038    }
039
040    /**
041     * Creates a new Front-End token request.
042     *
043     * @param redirectUrl The Redirect URI set in the Vonage Application.
044     * @param code The authentication code that is used to exchange for an access token.
045     */
046    public TokenRequest(URI redirectUrl, String code) {
047        params.put("grant_type", "authorization_code");
048        params.put("code", Objects.requireNonNull(code, "Code is required."));
049        params.put("redirect_uri", Objects.requireNonNull(redirectUrl, "Redirect URI is required.").toString());
050    }
051
052    @Override
053    public Map<String, String> makeParams() {
054        return params;
055    }
056}