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.ciba;
019
020
021import com.nimbusds.oauth2.sdk.AuthorizationGrant;
022import com.nimbusds.oauth2.sdk.GrantType;
023import com.nimbusds.oauth2.sdk.OAuth2Error;
024import com.nimbusds.oauth2.sdk.ParseException;
025import com.nimbusds.oauth2.sdk.util.MultivaluedMapUtils;
026import net.jcip.annotations.Immutable;
027
028import java.util.*;
029
030
031/**
032 * CIBA grant.
033 *
034 * <p>Related specifications:
035 *
036 * <ul>
037 *      <li>OpenID Connect CIBA Flow - Core 1.0
038 * </ul>
039 */
040@Immutable
041public class CIBAGrant extends AuthorizationGrant {
042        
043        
044        /**
045         * The grant type.
046         */
047        public static final GrantType GRANT_TYPE = GrantType.CIBA;
048        
049        
050        /**
051         * The authentication request ID.
052         */
053        private final AuthRequestID authRequestID;
054        
055        
056        /**
057         * Creates a new CIBA grant.
058         *
059         * @param authRequestID The authentication request ID. Must not be
060         *                      {@code null}.
061         */
062        public CIBAGrant(final AuthRequestID authRequestID) {
063                
064                super(GRANT_TYPE);
065                this.authRequestID = Objects.requireNonNull(authRequestID);
066        }
067        
068        
069        /**
070         * Returns the authentication request ID.
071         *
072         * @return The authentication request ID.
073         */
074        public AuthRequestID getAuthRequestID() {
075                
076                return authRequestID;
077        }
078        
079        
080        @Override
081        public Map<String, List<String>> toParameters() {
082
083                Map<String, List<String>> params = new LinkedHashMap<>();
084                params.put("grant_type", Collections.singletonList(GRANT_TYPE.getValue()));
085                params.put("auth_req_id", Collections.singletonList(authRequestID.getValue()));
086                return params;
087        }
088        
089        
090        @Override
091        public boolean equals(Object o) {
092                if (this == o)
093                        return true;
094                if (!(o instanceof CIBAGrant))
095                        return false;
096                CIBAGrant cibaGrant = (CIBAGrant) o;
097                return getAuthRequestID().equals(cibaGrant.getAuthRequestID());
098        }
099        
100        
101        @Override
102        public int hashCode() {
103                return Objects.hash(getAuthRequestID());
104        }
105        
106        
107        /**
108         * Parses a CIBA grant from the specified request body parameters.
109         *
110         * <p>Example:
111         *
112         * <pre>
113         * scope=openid%20email%20example-scope&amp;
114         * client_notification_token=8d67dc78-7faa-4d41-aabd-67707b374255&amp;
115         * binding_message=W4SCT&amp;
116         * login_hint_token=eyJraWQiOiJsdGFjZXNidyIsImFsZyI6IkVTMjU2In0.eyJ
117         * zdWJfaWQiOnsic3ViamVjdF90eXBlIjoicGhvbmUiLCJwaG9uZSI6IisxMzMwMjg
118         * xODAwNCJ9fQ.Kk8jcUbHjJAQkRSHyDuFQr3NMEOSJEZc85VfER74tX6J9CuUllr8
119         * 9WKUHUR7MA0-mWlptMRRhdgW1ZDt7g1uwQ&amp;
120         * client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3A&amp;
121         * client-assertion-type%3Ajwt-bearer&amp;
122         * client_assertion=eyJraWQiOiJsdGFjZXNidyIsImFsZyI6IkVTMjU2In0.eyJ
123         * pc3MiOiJzNkJoZFJrcXQzIiwic3ViIjoiczZCaGRSa3F0MyIsImF1ZCI6Imh0dHB
124         * zOi8vc2VydmVyLmV4YW1wbGUuY29tIiwianRpIjoiYmRjLVhzX3NmLTNZTW80RlN
125         * 6SUoyUSIsImlhdCI6MTUzNzgxOTQ4NiwiZXhwIjoxNTM3ODE5Nzc3fQ.Ybr8mg_3
126         * E2OptOSsA8rnelYO_y1L-yFaF_j1iemM3ntB61_GN3APe5cl_-5a6cvGlP154XAK
127         * 7fL-GaZSdnd9kg
128         * </pre>
129         *
130         * @param params The parameters.
131         *
132         * @return The CIBA grant.
133         *
134         * @throws ParseException If parsing failed.
135         */
136        public static CIBAGrant parse(final Map<String, List<String>> params) throws ParseException {
137                
138                GrantType.ensure(GRANT_TYPE, params);
139                
140                String authReqIDString = MultivaluedMapUtils.getFirstValue(params, "auth_req_id");
141                
142                if (authReqIDString == null || authReqIDString.trim().isEmpty()) {
143                        String msg = "Missing or empty auth_req_id parameter";
144                        throw new ParseException(msg, OAuth2Error.INVALID_REQUEST.appendDescription(": " + msg));
145                }
146                
147                AuthRequestID authRequestID = AuthRequestID.parse(authReqIDString);
148                
149                return new CIBAGrant(authRequestID);
150        }
151}