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