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 java.io.UnsupportedEncodingException; 022import java.net.URLDecoder; 023import java.net.URLEncoder; 024import java.nio.charset.Charset; 025 026import net.jcip.annotations.Immutable; 027 028import com.nimbusds.jose.util.Base64; 029 030import com.nimbusds.oauth2.sdk.ParseException; 031import com.nimbusds.oauth2.sdk.id.ClientID; 032import com.nimbusds.oauth2.sdk.http.HTTPRequest; 033 034 035/** 036 * Client secret basic authentication at the Token endpoint. Implements 037 * {@link ClientAuthenticationMethod#CLIENT_SECRET_BASIC}. 038 * 039 * <p>Example HTTP Authorization header (for client identifier "s6BhdRkqt3" and 040 * secret "7Fjfp0ZBr1KtDRbnfVdmIw"): 041 * 042 * <pre> 043 * Authorization: Basic czZCaGRSa3F0Mzo3RmpmcDBaQnIxS3REUmJuZlZkbUl3 044 * </pre> 045 * 046 * <p>Related specifications: 047 * 048 * <p>Related specifications: 049 * 050 * <ul> 051 * <li>OAuth 2.0 (RFC 6749), sections 2.3.1 and 3.2.1. 052 * <li>OpenID Connect Core 1.0, section 9. 053 * <li>HTTP Authentication: Basic and Digest Access Authentication 054 * (RFC 2617). 055 * </ul> 056 */ 057@Immutable 058public final class ClientSecretBasic extends PlainClientSecret { 059 060 061 /** 062 * The default character set for the client ID and secret encoding. 063 */ 064 private static final Charset UTF8_CHARSET = Charset.forName("UTF-8"); 065 066 067 /** 068 * Creates a new client secret basic authentication. 069 * 070 * @param clientID The client identifier. Must not be {@code null}. 071 * @param secret The client secret. Must not be {@code null}. 072 */ 073 public ClientSecretBasic(final ClientID clientID, final Secret secret) { 074 075 super(ClientAuthenticationMethod.CLIENT_SECRET_BASIC, clientID, secret); 076 } 077 078 079 /** 080 * Returns the HTTP Authorization header representation of this client 081 * secret basic authentication. 082 * 083 * <p>Note that OAuth 2.0 (RFC 6749, section 2.3.1) requires the client 084 * ID and secret to be {@code application/x-www-form-urlencoded} before 085 * passing them to the HTTP basic authentication algorithm. This 086 * behaviour differs from the original HTTP Basic Authentication 087 * specification (RFC 2617). 088 * 089 * <p>Example HTTP Authorization header (for client identifier 090 * "Aladdin" and password "open sesame"): 091 * 092 * <pre> 093 * 094 * Authorization: Basic QWxhZGRpbjpvcGVuK3Nlc2FtZQ== 095 * </pre> 096 * 097 * <p>See RFC 2617, section 2. 098 * 099 * @return The HTTP Authorization header. 100 */ 101 public String toHTTPAuthorizationHeader() { 102 103 StringBuilder sb = new StringBuilder(); 104 105 try { 106 sb.append(URLEncoder.encode(getClientID().getValue(), UTF8_CHARSET.name())); 107 sb.append(':'); 108 sb.append(URLEncoder.encode(getClientSecret().getValue(), UTF8_CHARSET.name())); 109 110 } catch (UnsupportedEncodingException e) { 111 112 // UTF-8 should always be supported 113 } 114 115 return "Basic " + Base64.encode(sb.toString().getBytes(UTF8_CHARSET)); 116 } 117 118 119 @Override 120 public void applyTo(final HTTPRequest httpRequest) { 121 122 httpRequest.setAuthorization(toHTTPAuthorizationHeader()); 123 } 124 125 126 /** 127 * Parses a client secret basic authentication from the specified HTTP 128 * Authorization header. 129 * 130 * @param header The HTTP Authorization header to parse. Must not be 131 * {@code null}. 132 * 133 * @return The client secret basic authentication. 134 * 135 * @throws ParseException If the header couldn't be parsed to a client 136 * secret basic authentication. 137 */ 138 public static ClientSecretBasic parse(final String header) 139 throws ParseException { 140 141 String[] parts = header.split("\\s"); 142 143 if (parts.length != 2) 144 throw new ParseException("Malformed client secret basic authentication (see RFC 6749, section 2.3.1): Unexpected number of HTTP Authorization header value parts: " + parts.length); 145 146 if (! parts[0].equalsIgnoreCase("Basic")) 147 throw new ParseException("HTTP authentication must be \"Basic\""); 148 149 String credentialsString = new String(new Base64(parts[1]).decode(), UTF8_CHARSET); 150 151 String[] credentials = credentialsString.split(":", 2); 152 153 if (credentials.length != 2) 154 throw new ParseException("Malformed client secret basic authentication (see RFC 6749, section 2.3.1): Missing credentials delimiter \":\""); 155 156 try { 157 String decodedClientID = URLDecoder.decode(credentials[0], UTF8_CHARSET.name()); 158 String decodedSecret = URLDecoder.decode(credentials[1], UTF8_CHARSET.name()); 159 160 return new ClientSecretBasic(new ClientID(decodedClientID), new Secret(decodedSecret)); 161 162 } catch (UnsupportedEncodingException e) { 163 164 throw new ParseException(e.getMessage(), e); 165 } 166 } 167 168 169 /** 170 * Parses a client secret basic authentication from the specified HTTP 171 * request. 172 * 173 * @param httpRequest The HTTP request to parse. Must not be 174 * {@code null} and must contain a valid 175 * Authorization header. 176 * 177 * @return The client secret basic authentication. 178 * 179 * @throws ParseException If the HTTP Authorization header couldn't be 180 * parsed to a client secret basic 181 * authentication. 182 */ 183 public static ClientSecretBasic parse(final HTTPRequest httpRequest) 184 throws ParseException { 185 186 String header = httpRequest.getAuthorization(); 187 188 if (header == null) 189 throw new ParseException("Missing HTTP Authorization header"); 190 191 return parse(header); 192 } 193}