001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.oauth2.sdk.auth;
019
020
021import com.nimbusds.oauth2.sdk.id.Identifier;
022import net.jcip.annotations.Immutable;
023
024
025/**
026 * Client authentication method at the Token endpoint.
027 *
028 * <p>Constants are provided for four client authentication methods:
029 *
030 * <ul>
031 *     <li>{@link #CLIENT_SECRET_BASIC client_secret_basic} (default)
032 *     <li>{@link #CLIENT_SECRET_POST client_secret_post}
033 *     <li>{@link #CLIENT_SECRET_JWT client_secret_jwt}
034 *     <li>{@link #PRIVATE_KEY_JWT private_key_jwt}
035 *     <li>{@link #TLS_CLIENT_AUTH tls_client_auth}
036 *     <li>{@link #SELF_SIGNED_TLS_CLIENT_AUTH self_signed_tls_client_auth}
037 *     <li>{@link #REQUEST_OBJECT request_object}
038 *     <li>{@link #NONE none}
039 * </ul>
040 *
041 * <p>Use the constructor to define a custom client authentication method.
042 *
043 * <p>Related specifications:
044 *
045 * <ul>
046 *     <li>OAuth 2.0 (RFC 6749)
047 *     <li>OAuth 2.0 Dynamic Client Registration Protocol (RFC 7591)
048 *     <li>OAuth 2.0 Mutual TLS Client Authentication and Certificate Bound
049 *         Access Tokens (RFC 8705)
050 *     <li>OpenID Connect Federation 1.0.
051 * </ul>
052 */
053@Immutable
054public final class ClientAuthenticationMethod extends Identifier {
055        
056        
057        private static final long serialVersionUID = 1L;
058
059
060        /**
061         * Clients that have received a client secret from the authorisation 
062         * server authenticate with the authorisation server in accordance with
063         * section 3.2.1 of OAuth 2.0 using HTTP Basic authentication. This is 
064         * the default if no method has been registered for the client.
065         */
066        public static final ClientAuthenticationMethod CLIENT_SECRET_BASIC = 
067                new ClientAuthenticationMethod("client_secret_basic");
068
069
070        /**
071         * Clients that have received a client secret from the authorisation 
072         * server authenticate with the authorisation server in accordance with
073         * section 3.2.1 of OAuth 2.0 by including the client credentials in 
074         * the request body.
075         */
076        public static final ClientAuthenticationMethod CLIENT_SECRET_POST =
077                new ClientAuthenticationMethod("client_secret_post");
078
079
080        /**
081         * Clients that have received a client secret from the authorisation 
082         * server, create a JWT using an HMAC SHA algorithm, such as HMAC 
083         * SHA-256. The HMAC (Hash-based Message Authentication Code) is
084         * calculated using the value of client secret as the shared key. The 
085         * client authenticates in accordance with section 2.2 of (JWT) Bearer
086         * Token Profiles and OAuth 2.0 Assertion Profile. 
087         */
088        public static final ClientAuthenticationMethod CLIENT_SECRET_JWT =
089                new ClientAuthenticationMethod("client_secret_jwt");
090
091
092        /**
093         * Clients that have registered a public key sign a JWT using the RSA 
094         * algorithm if a RSA key was registered or the ECDSA algorithm if an 
095         * Elliptic Curve key was registered (see JWA for the algorithm 
096         * identifiers). The client authenticates in accordance with section 
097         * 2.2 of (JWT) Bearer Token Profiles and OAuth 2.0 Assertion Profile.
098         */
099        public static final ClientAuthenticationMethod PRIVATE_KEY_JWT =
100                new ClientAuthenticationMethod("private_key_jwt");
101        
102        
103        /**
104         * PKI mutual TLS OAuth client authentication. See OAuth 2.0 Mutual TLS
105         * Client Authentication and Certificate Bound Access Tokens (RFC
106         * 8705), section 2.1.
107         */
108        public static final ClientAuthenticationMethod TLS_CLIENT_AUTH =
109                new ClientAuthenticationMethod("tls_client_auth");
110        
111        
112        /**
113         * Self-signed certificate mutual TLS OAuth client authentication. See
114         * OAuth 2.0 Mutual TLS Client Authentication and Certificate Bound
115         * Access Tokens (RFC 8705), section 2.2.
116         */
117        public static final ClientAuthenticationMethod SELF_SIGNED_TLS_CLIENT_AUTH =
118                new ClientAuthenticationMethod("self_signed_tls_client_auth");
119        
120        
121        /**
122         * Client verification by means of a request object at the
123         * authorisation or PAR endpoints. Intended for OpenID Connect
124         * Federation 1.0 clients undertaking automatic registration. See
125         * OpenID Connect Federation 1.0.
126         */
127        public static final ClientAuthenticationMethod REQUEST_OBJECT =
128                new ClientAuthenticationMethod("request_object");
129
130
131        /**
132         * The client is a public client as defined in OAuth 2.0 and does not
133         * have a client secret.
134         */
135        public static final ClientAuthenticationMethod NONE =
136                new ClientAuthenticationMethod("none");
137
138
139        /**
140         * Gets the default client authentication method.
141         *
142         * @return {@link #CLIENT_SECRET_BASIC}
143         */
144        public static ClientAuthenticationMethod getDefault() {
145
146                return CLIENT_SECRET_BASIC;
147        }
148
149
150        /**
151         * Creates a new client authentication method with the specified value.
152         *
153         * @param value The authentication method value. Must not be 
154         *              {@code null} or empty string.
155         */
156        public ClientAuthenticationMethod(final String value) {
157
158                super(value);
159        }
160
161
162        /**
163         * Parses a client authentication method from the specified value.
164         *
165         * @param value The authentication method value. Must not be
166         *              {@code null} or empty string.
167         *
168         * @return The client authentication method.
169         */
170        public static ClientAuthenticationMethod parse(final String value) {
171
172                if (value.equals(CLIENT_SECRET_BASIC.getValue())) {
173                        return CLIENT_SECRET_BASIC;
174                } else if (value.equals(CLIENT_SECRET_POST.getValue())) {
175                        return CLIENT_SECRET_POST;
176                } else if (value.equals(CLIENT_SECRET_JWT.getValue())) {
177                        return CLIENT_SECRET_JWT;
178                } else if (value.equals(PRIVATE_KEY_JWT.getValue())) {
179                        return PRIVATE_KEY_JWT;
180                } else if (value.equalsIgnoreCase(TLS_CLIENT_AUTH.getValue())) {
181                        return TLS_CLIENT_AUTH;
182                } else if (value.equalsIgnoreCase(SELF_SIGNED_TLS_CLIENT_AUTH.getValue())) {
183                        return SELF_SIGNED_TLS_CLIENT_AUTH;
184                } else if (value.equalsIgnoreCase(REQUEST_OBJECT.getValue())) {
185                        return REQUEST_OBJECT;
186                } else if (value.equals(NONE.getValue())) {
187                        return NONE;
188                } else {
189                        return new ClientAuthenticationMethod(value);
190                }
191        }
192
193
194        @Override
195        public boolean equals(final Object object) {
196        
197                return object instanceof ClientAuthenticationMethod &&
198                       this.toString().equals(object.toString());
199        }
200}