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; 019 020 021import java.util.Collections; 022import java.util.LinkedHashMap; 023import java.util.List; 024import java.util.Map; 025 026import com.nimbusds.oauth2.sdk.util.MultivaluedMapUtils; 027import net.jcip.annotations.Immutable; 028 029 030/** 031 * Client credentials grant. Used in access token requests where the client is 032 * acting on its own behalf. 033 * 034 * <p>Related specifications: 035 * 036 * <ul> 037 * <li>OAuth 2.0 (RFC 6749), section 4.4.2. 038 * </ul> 039 */ 040@Immutable 041public class ClientCredentialsGrant extends AuthorizationGrant { 042 043 044 /** 045 * The grant type. 046 */ 047 public static final GrantType GRANT_TYPE = GrantType.CLIENT_CREDENTIALS; 048 049 050 /** 051 * Creates a new client credentials grant. The actual client 052 * credentials are included in the 053 * {@link com.nimbusds.oauth2.sdk.auth.ClientAuthentication client 054 * authentication} of the {@link com.nimbusds.oauth2.sdk.TokenRequest}. 055 */ 056 public ClientCredentialsGrant() { 057 058 super(GRANT_TYPE); 059 } 060 061 062 @Override 063 public Map<String,List<String>> toParameters() { 064 065 Map<String,List<String>> params = new LinkedHashMap<>(); 066 params.put("grant_type", Collections.singletonList(GRANT_TYPE.getValue())); 067 return params; 068 } 069 070 071 /** 072 * Parses a client credentials grant from the specified request body 073 * parameters. 074 * 075 * <p>Example: 076 * 077 * <pre> 078 * grant_type=client_credentials 079 * </pre> 080 * 081 * @param params The parameters. 082 * 083 * @return The client credentials grant. 084 * 085 * @throws ParseException If parsing failed. 086 */ 087 public static ClientCredentialsGrant parse(final Map<String,List<String>> params) 088 throws ParseException { 089 090 // Parse grant type 091 String grantTypeString = MultivaluedMapUtils.getFirstValue(params, "grant_type"); 092 093 if (grantTypeString == null) { 094 String msg = "Missing \"grant_type\" parameter"; 095 throw new ParseException(msg, OAuth2Error.INVALID_REQUEST.appendDescription(": " + msg)); 096 } 097 098 if (! GrantType.parse(grantTypeString).equals(GRANT_TYPE)) { 099 String msg = "The \"grant_type\" must be " + GRANT_TYPE; 100 throw new ParseException(msg, OAuth2Error.UNSUPPORTED_GRANT_TYPE.appendDescription(": " + msg)); 101 } 102 103 return new ClientCredentialsGrant(); 104 } 105}