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 *     <li>{@link GrantType#JWT_BEARER}
018 *     <li>{@link GrantType#SAML2_BEARER}
019 * </ul>
020 *
021 * <p>Related specifications:
022 *
023 * <ul>
024 *     <li>OAuth 2.0 (RFC 6749), sections 1.3.
025 * </ul>
026 */
027public abstract class AuthorizationGrant {
028
029
030        /**
031         * The authorisation grant type.
032         */
033        private final GrantType type;
034
035
036        /**
037         * Creates a new authorisation grant.
038         *
039         * @param type               The authorisation grant type. Must not be
040         *                           {@code null}.
041         */
042        protected AuthorizationGrant(final GrantType type) {
043
044                if (type == null)
045                        throw new IllegalArgumentException("The grant type must not be null");
046
047                this.type = type;
048        }
049
050
051        /**
052         * Gets the authorisation grant type.
053         *
054         * @return The authorisation grant type.
055         */
056        public GrantType getType() {
057
058                return type;
059        }
060
061
062        /**
063         * Return the parameters for the authorisation grant.
064         *
065         * @return The parameters.
066         */
067        public abstract Map<String,String> toParameters();
068
069
070        /**
071         * Parses an authorisation grant from the specified parameters.
072         *
073         * @param params The parameters. Must not be {@code null}.
074         *
075         * @return The authorisation grant.
076         *
077         * @throws ParseException If parsing failed or the grant type is not
078         *                        supported.
079         */
080        public static AuthorizationGrant parse(final Map<String,String> params)
081                throws ParseException {
082
083                // Parse grant type
084                String grantTypeString = params.get("grant_type");
085
086                if (grantTypeString == null)
087                        throw new ParseException("Missing \"grant_type\" parameter", OAuth2Error.INVALID_REQUEST);
088
089                GrantType grantType = GrantType.parse(grantTypeString);
090
091                if (grantType.equals(GrantType.AUTHORIZATION_CODE)) {
092
093                        return AuthorizationCodeGrant.parse(params);
094
095                } else if (grantType.equals(GrantType.REFRESH_TOKEN)) {
096
097                        return RefreshTokenGrant.parse(params);
098                        
099                } else if (grantType.equals(GrantType.PASSWORD)) {
100
101                        return ResourceOwnerPasswordCredentialsGrant.parse(params);
102
103                } else if (grantType.equals(GrantType.CLIENT_CREDENTIALS)) {
104
105                        return ClientCredentialsGrant.parse(params);
106
107                } else if (grantType.equals(GrantType.JWT_BEARER)) {
108
109                        return JWTBearerGrant.parse(params);
110
111                } else if (grantType.equals(GrantType.SAML2_BEARER)) {
112
113                        return SAML2BearerGrant.parse(params);
114
115                } else {
116
117                        throw new ParseException("Invalid or unsupported grant type: " + grantType, OAuth2Error.UNSUPPORTED_GRANT_TYPE);
118                }
119        }
120}