001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Portions copyright 2011-2013 The MITRE Corporation
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the 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
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *******************************************************************************/
018/**
019 *
020 */
021package org.mitre.openid.connect.client.service.impl;
022
023import java.net.URISyntaxException;
024import java.util.Map;
025import java.util.Map.Entry;
026
027import org.apache.http.client.utils.URIBuilder;
028import org.mitre.oauth2.model.RegisteredClient;
029import org.mitre.openid.connect.client.service.AuthRequestUrlBuilder;
030import org.mitre.openid.connect.config.ServerConfiguration;
031import org.springframework.security.authentication.AuthenticationServiceException;
032
033import com.google.common.base.Joiner;
034import com.google.common.base.Strings;
035
036/**
037 *
038 * Builds an auth request redirect URI with normal query parameters.
039 *
040 * @author jricher
041 *
042 */
043public class PlainAuthRequestUrlBuilder implements AuthRequestUrlBuilder {
044
045        /* (non-Javadoc)
046         * @see org.mitre.openid.connect.client.service.AuthRequestUrlBuilder#buildAuthRequest(javax.servlet.http.HttpServletRequest, org.mitre.openid.connect.config.ServerConfiguration, org.springframework.security.oauth2.provider.ClientDetails)
047         */
048        @Override
049        public String buildAuthRequestUrl(ServerConfiguration serverConfig, RegisteredClient clientConfig, String redirectUri, String nonce, String state, Map<String, String> options, String loginHint) {
050                try {
051
052                        URIBuilder uriBuilder = new URIBuilder(serverConfig.getAuthorizationEndpointUri());
053                        uriBuilder.addParameter("response_type", "code");
054                        uriBuilder.addParameter("client_id", clientConfig.getClientId());
055                        uriBuilder.addParameter("scope", Joiner.on(" ").join(clientConfig.getScope()));
056
057                        uriBuilder.addParameter("redirect_uri", redirectUri);
058
059                        uriBuilder.addParameter("nonce", nonce);
060
061                        uriBuilder.addParameter("state", state);
062
063                        // Optional parameters:
064                        for (Entry<String, String> option : options.entrySet()) {
065                                uriBuilder.addParameter(option.getKey(), option.getValue());
066                        }
067
068                        // if there's a login hint, send it
069                        if (!Strings.isNullOrEmpty(loginHint)) {
070                                uriBuilder.addParameter("login_hint", loginHint);
071                        }
072
073                        return uriBuilder.build().toString();
074
075                } catch (URISyntaxException e) {
076                        throw new AuthenticationServiceException("Malformed Authorization Endpoint Uri", e);
077
078                }
079
080
081
082        }
083
084}