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.DynamicEndpoint;
019import com.vonage.client.HttpWrapper;
020import com.vonage.client.RestEndpoint;
021import com.vonage.client.auth.JWTAuthMethod;
022import com.vonage.client.auth.NoAuthMethod;
023import com.vonage.client.common.HttpMethod;
024import java.net.URI;
025import java.util.Objects;
026
027/**
028 * Used for obtaining access tokens for use with Vonage CAMARA APIs.
029 */
030public class NetworkAuthClient {
031    final RestEndpoint<BackendAuthRequest, BackendAuthResponse> backendAuth;
032    final RestEndpoint<TokenRequest, TokenResponse> tokenRequest;
033
034    /**
035     * Create a new NetworkAuthClient.
036     *
037     * @param wrapper Http Wrapper used to create authentication requests.
038     */
039    public NetworkAuthClient(HttpWrapper wrapper) {
040        @SuppressWarnings("unchecked")
041        final class Endpoint<T, R> extends DynamicEndpoint<T, R> {
042            Endpoint(String path, HttpMethod method, R... type) {
043                super(DynamicEndpoint.<T, R> builder(type)
044                        .responseExceptionType(NetworkAuthResponseException.class)
045                        .wrapper(wrapper).requestMethod(method).urlFormEncodedContentType(true)
046                        .authMethod(JWTAuthMethod.class)
047                        .pathGetter((de, req) -> de.getHttpWrapper().getHttpConfig()
048                                .getApiEuBaseUri() + "/oauth2/" + path
049                        )
050                );
051            }
052        }
053
054        backendAuth = new Endpoint<>("bc-authorize", HttpMethod.POST);
055        tokenRequest = new Endpoint<>("token", HttpMethod.POST);
056    }
057
058    private <R> R validateRequest(R request) {
059        return Objects.requireNonNull(request, "Request is required.");
060    }
061
062    public BackendAuthResponse buildOidcUrl(BackendAuthRequest request) {
063        return backendAuth.execute(validateRequest(request));
064    }
065
066    /**
067     * Obtains a new access token for a Back-End auth request.
068     *
069     * @param request The token request parameters.
070     * @return The response containing the access token.
071     * @throws NetworkAuthResponseException If an error was encountered during the workflow.
072     * @since 8.9.0
073     */
074    public TokenResponse getCamaraToken(TokenRequest request) {
075        return tokenRequest.execute(validateRequest(request));
076    }
077}