001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2020, 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.openid.connect.sdk.federation.api;
019
020
021import com.nimbusds.oauth2.sdk.ParseException;
022import com.nimbusds.oauth2.sdk.http.HTTPRequest;
023import com.nimbusds.oauth2.sdk.id.Issuer;
024import com.nimbusds.oauth2.sdk.id.Subject;
025import com.nimbusds.oauth2.sdk.util.MultivaluedMapUtils;
026import com.nimbusds.oauth2.sdk.util.URIUtils;
027import com.nimbusds.openid.connect.sdk.federation.entities.EntityID;
028import net.jcip.annotations.Immutable;
029
030import java.net.URI;
031import java.util.Collections;
032import java.util.HashMap;
033import java.util.List;
034import java.util.Map;
035
036
037/**
038 * Fetch entity statement request.
039 *
040 * <p>Related specifications:
041 *
042 * <ul>
043 *     <li>OpenID Connect Federation 1.0, section 7.1.1.
044 * </ul>
045 */
046@Immutable
047public class FetchEntityStatementRequest extends FederationAPIRequest {
048        
049        
050        /**
051         * The optional issuer.
052         */
053        private final Issuer issuer;
054        
055        
056        /**
057         * The optional subject.
058         */
059        private final Subject subject;
060        
061        
062        /**
063         * Creates a new fetch entity request.
064         *
065         * @param endpoint The federation fetch endpoint. Must not be
066         *                 {@code null}.
067         * @param issuer   The issuer entity identifier, {@code null} if not
068         *                 specified.
069         * @param subject  The subject entity identifier, {@code null} if not
070         *                 specified.
071         */
072        public FetchEntityStatementRequest(final URI endpoint, final Issuer issuer, final Subject subject) {
073                super(endpoint);
074                this.issuer = issuer;
075                this.subject = subject;
076        }
077        
078        
079        /**
080         * Creates a new entity fetch request.
081         *
082         * @param endpoint The federation fetch endpoint. Must not be
083         *                 {@code null}.
084         * @param issuer   The issuer entity identifier, {@code null} if not
085         *                 specified.
086         * @param subject  The subject entity identifier, {@code null} if not
087         *                 specified.
088         */
089        public FetchEntityStatementRequest(final URI endpoint, final EntityID issuer, final EntityID subject) {
090                this(
091                        endpoint,
092                        issuer != null ? new Issuer(issuer.getValue()) : null,
093                        subject != null ? new Subject(subject.getValue()) : null
094                );
095        }
096        
097        
098        /**
099         * Returns the issuer.
100         *
101         * @return The issuer, {@code null} if not specified.
102         */
103        public Issuer getIssuer() {
104                return issuer;
105        }
106        
107        
108        /**
109         * Returns the issuer entity ID.
110         *
111         * @return The issuer entity ID, {@code null} if not specified.
112         */
113        public EntityID getIssuerEntityID() {
114                return getIssuer() != null ? new EntityID(getIssuer().getValue()) : null;
115        }
116        
117        
118        /**
119         * Returns the optional subject.
120         *
121         * @return The subject, {@code null} if not specified.
122         */
123        public Subject getSubject() {
124                return subject;
125        }
126        
127        
128        /**
129         * Returns the optional subject entity ID.
130         *
131         * @return The subject entity ID, {@code null} if not specified.
132         */
133        public EntityID getSubjectEntityID() {
134                return getSubject() != null ? new EntityID(getSubject().getValue()) : null;
135        }
136        
137        
138        @Override
139        public Map<String, List<String>> toParameters() {
140                
141                Map<String, List<String>> params = new HashMap<>();
142                if (getIssuer() != null) {
143                        params.put("iss", Collections.singletonList(getIssuer().getValue()));
144                }
145                if (getSubject() != null) {
146                        params.put("sub", Collections.singletonList(getSubject().getValue()));
147                }
148                return params;
149        }
150        
151        
152        /**
153         * Parses a fetch entity statement request from the specified query
154         * string parameters.
155         *
156         * @param params The query string parameters. Must not be {@code null}.
157         *
158         * @return The fetch entity statement request.
159         *
160         * @throws ParseException If parsing failed.
161         */
162        public static FetchEntityStatementRequest parse(final Map<String, List<String>> params)
163                throws ParseException {
164                
165                String value = MultivaluedMapUtils.getFirstValue(params, "iss");
166                Issuer issuer = null;
167                if (value != null) {
168                        issuer = new Issuer(value);
169                }
170                
171                value = MultivaluedMapUtils.getFirstValue(params, "sub");
172                Subject subject = null;
173                if (value != null) {
174                        subject = new Subject(value);
175                }
176                
177                return new FetchEntityStatementRequest(null, issuer, subject);
178        }
179        
180        
181        /**
182         * Parses a fetch entity statement request from the specified HTTP
183         * request.
184         *
185         * @param httpRequest The HTTP request. Must not be {@code null}.
186         *
187         * @return The fetch entity statement request.
188         *
189         * @throws ParseException If parsing failed.
190         */
191        public static FetchEntityStatementRequest parse(final HTTPRequest httpRequest)
192                throws ParseException {
193                
194                httpRequest.ensureMethod(HTTPRequest.Method.GET);
195                FetchEntityStatementRequest request = parse(httpRequest.getQueryStringParameters());
196                return new FetchEntityStatementRequest(
197                        URIUtils.getBaseURI(httpRequest.getURI()),
198                        request.getIssuer(),
199                        request.getSubject());
200        }
201}