001package com.nimbusds.oauth2.sdk;
002
003
004import java.util.Map;
005
006
007/**
008 * Authorisation grant. Extending classes should be immutable.
009 *
010 * <p>Supported authorisation grant types:
011 *
012 * <ul>
013 *     <li>{@link GrantType#AUTHORIZATION_CODE Authorisation code}
014 *     <li>{@link GrantType#PASSWORD Resource owner password credentials}
015 *     <li>{@link GrantType#CLIENT_CREDENTIALS Client credentials}
016 *     <li>{@link GrantType#REFRESH_TOKEN Refresh token}
017 * </ul>
018 *
019 * <p>Related specifications:
020 *
021 * <ul>
022 *     <li>OAuth 2.0 (RFC 6749), sections 1.3.
023 * </ul>
024 */
025public abstract class AuthorizationGrant {
026
027
028        /**
029         * The authorisation grant type.
030         */
031        private final GrantType type;
032
033
034        /**
035         * Creates a new authorisation grant.
036         *
037         * @param type The authorisation grant type. Must not be {@code null}.
038         */
039        protected AuthorizationGrant(final GrantType type) {
040
041                if (type == null)
042                        throw new IllegalArgumentException("The grant type must not be null");
043
044                this.type = type;
045        }
046
047
048        /**
049         * Gets the authorisation grant type.
050         *
051         * @return The authorisation grant type.
052         */
053        public GrantType getType() {
054
055                return type;
056        }
057
058
059        /**
060         * Return the parameters for the authorisation grant.
061         *
062         * @return The parameters.
063         */
064        public abstract Map<String,String> toParameters();
065
066
067        /**
068         * Parses an authorisation grant from the specified parameters.
069         *
070         * @param params The parameters. Must not be {@code null}.
071         *
072         * @return The authorisation grant.
073         *
074         * @throws ParseException If parsing failed or the grant type is not
075         *                        supported.
076         */
077        public static AuthorizationGrant parse(final Map<String,String> params)
078                throws ParseException {
079
080                // Parse grant type
081                String grantTypeString = params.get("grant_type");
082
083                if (grantTypeString == null)
084                        throw new ParseException("Missing \"grant_type\" parameter", OAuth2Error.INVALID_REQUEST);
085
086                GrantType grantType = new GrantType(grantTypeString);
087
088                if (grantType.equals(GrantType.AUTHORIZATION_CODE)) {
089
090                        return AuthorizationCodeGrant.parse(params);
091
092                } else if (grantType.equals(GrantType.REFRESH_TOKEN)) {
093
094                        return RefreshTokenGrant.parse(params);
095                }
096
097                else if (grantType.equals(GrantType.PASSWORD)) {
098
099                        return ResourceOwnerPasswordCredentialsGrant.parse(params);
100
101                } else if (grantType.equals(GrantType.CLIENT_CREDENTIALS)) {
102
103                        return ClientCredentialsGrant.parse(params);
104
105                } else {
106
107                        throw new ParseException("Unsupported grant type: " + grantType, OAuth2Error.UNSUPPORTED_GRANT_TYPE);
108                }
109        }
110}