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