001package com.nimbusds.oauth2.sdk;
002
003
004import java.util.LinkedHashMap;
005import java.util.Map;
006
007import net.jcip.annotations.Immutable;
008
009
010/**
011 * Client credentials grant. Used in access token requests with a client's
012 * identifier and secret.
013 *
014 * <p>Related specifications:
015 *
016 * <ul>
017 *     <li>OAuth 2.0 (RFC 6749), section 4.4.2.
018 * </ul>
019 */
020@Immutable
021public class ClientCredentialsGrant extends AuthorizationGrant {
022
023
024        /**
025         * The associated grant type.
026         */
027        public static final GrantType GRANT_TYPE = GrantType.CLIENT_CREDENTIALS;
028
029
030        /**
031         * The requested scope.
032         */
033        private final Scope scope;
034
035
036        /**
037         * Creates a new client credentials grant.
038         *
039         * @param scope The requested scope, {@code null} if not specified.
040         */
041        public ClientCredentialsGrant(final Scope scope) {
042
043                super(GRANT_TYPE);
044
045                this.scope = scope;
046        }
047
048
049        /**
050         * Gets the requested scope.
051         *
052         * @return The requested scope, {@code null} if not specified.
053         */
054        public Scope getScope() {
055
056                return scope;
057        }
058
059
060        @Override
061        public Map<String,String> toParameters() {
062
063                Map<String,String> params = new LinkedHashMap<String,String>();
064
065                params.put("grant_type", GRANT_TYPE.getValue());
066
067                if (scope != null)
068                        params.put("scope", scope.toString());
069
070                return params;
071        }
072
073
074        /**
075         * Parses a client credentials grant from the specified parameters.
076         *
077         * <p>Example:
078         *
079         * <pre>
080         * grant_type=client_credentials
081         * </pre>
082         *
083         * @param params The parameters.
084         *
085         * @return The client credentials grant.
086         *
087         * @throws ParseException If parsing failed.
088         */
089        public static ClientCredentialsGrant parse(final Map<String,String> params)
090                throws ParseException {
091
092                // Parse grant type
093                String grantTypeString = params.get("grant_type");
094
095                if (grantTypeString == null)
096                        throw new ParseException("Missing \"grant_type\" parameter", OAuth2Error.INVALID_REQUEST);
097
098                GrantType grantType = new GrantType(grantTypeString);
099
100                if (! grantType.equals(GRANT_TYPE))
101                        throw new ParseException("The \"grant_type\" must be " + GRANT_TYPE, OAuth2Error.INVALID_GRANT);
102
103                // Parse optional scope
104                String scopeValue = params.get("scope");
105
106                Scope scope = null;
107
108                if (scopeValue != null)
109                        scope = Scope.parse(scopeValue);
110
111                return new ClientCredentialsGrant(scope);
112        }
113}