/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.am.gateway.handler.vertx.handler.oidc;

import io.gravitee.am.gateway.handler.oauth2.token.TokenService;
import io.gravitee.am.gateway.handler.oidc.discovery.OpenIDDiscoveryService;
import io.gravitee.am.gateway.handler.vertx.handler.oidc.endpoint.ProviderConfigurationEndpoint;
import io.gravitee.am.gateway.handler.vertx.handler.oidc.endpoint.UserInfoEndpoint;
import io.gravitee.am.gateway.handler.vertx.handler.oidc.handler.UserInfoRequestParseHandler;
import io.gravitee.am.gateway.service.UserService;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpMethod;
import io.vertx.reactivex.ext.web.Router;
import io.vertx.reactivex.ext.web.RoutingContext;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author David BRASSELY (david.brassely at graviteesource.com)
 * @author Titouan COMPIEGNE (titouan.compiegne at graviteesource.com)
 * @author GraviteeSource Team
 */
public class OIDCRouter {

    @Autowired
    private OpenIDDiscoveryService discoveryService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private UserService userService;

    public void route(Router router) {
        // OpenID Provider Configuration Information Endpoint
        Handler<RoutingContext> openIDProviderConfigurationEndpoint = new ProviderConfigurationEndpoint();
        ((ProviderConfigurationEndpoint) openIDProviderConfigurationEndpoint).setDiscoveryService(discoveryService);
        router
                .route(HttpMethod.GET, "/.well-known/openid-configuration")
                .handler(openIDProviderConfigurationEndpoint);

        // UserInfo Endpoint
        Handler<RoutingContext> userInfoEndpoint = new UserInfoEndpoint(userService);
        Handler<RoutingContext> userInfoRequestParseHandler = new UserInfoRequestParseHandler(tokenService);
        router
                .route(HttpMethod.GET, "/userinfo")
                .handler(userInfoRequestParseHandler)
                .handler(userInfoEndpoint);
        router
                .route(HttpMethod.POST, "/userinfo")
                .handler(userInfoRequestParseHandler)
                .handler(userInfoEndpoint);
    }
}

@@ -1312,16 +1312,71 @@
 ontext;%0A
+import io.vertx.reactivex.ext.web.handler.CorsHandler;%0A
 import o
@@ -1429,16 +1429,190 @@
 towired;
+%0Aimport org.springframework.core.env.Environment;%0A%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0Aimport java.util.stream.Collectors;%0A%0Aimport static java.util.Arrays.asList;
 %0A%0A/**%0A *
@@ -1980,24 +1980,77 @@
 erService;%0A%0A
+    @Autowired%0A    private Environment environment;%0A%0A
     public v
@@ -2733,32 +2733,115 @@
 (tokenService);%0A
+        router.route(%22/userinfo%22).handler(CorsHandler.newInstance(corsHandler()));%0A
         router%0A 
@@ -3103,32 +3103,32 @@
 stParseHandler)%0A
-
                 
@@ -3160,11 +3160,1418 @@
 );%0A    %7D
+%0A%0A    private io.vertx.ext.web.handler.CorsHandler corsHandler() %7B%0A        return io.vertx.ext.web.handler.CorsHandler%0A                .create(environment.getProperty(%22http.cors.allow-origin%22, String.class, %22*%22))%0A                .allowedHeaders(getStringPropertiesAsList(%22http.cors.allow-headers%22, %22Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With, If-Match, x-xsrf-token%22))%0A                .allowedMethods(getHttpMethodPropertiesAsList(%22http.cors.allow-methods%22, %22GET, POST%22))%0A                .maxAgeSeconds(environment.getProperty(%22http.cors.max-age%22, Integer.class, 86400));%0A    %7D%0A%0A    private Set%3CString%3E getStringPropertiesAsList(final String propertyKey, final String defaultValue) %7B%0A        String property = environment.getProperty(propertyKey);%0A        if (property == null) %7B%0A            property = defaultValue;%0A        %7D%0A        return new HashSet%3C%3E(asList(property.replaceAll(%22%5C%5Cs+%22,%22%22).split(%22,%22)));%0A    %7D%0A%0A    private Set%3CHttpMethod%3E getHttpMethodPropertiesAsList(final String propertyKey, final String defaultValue) %7B%0A        String property = environment.getProperty(propertyKey);%0A        if (property == null) %7B%0A            property = defaultValue;%0A        %7D%0A%0A        return asList(property.replaceAll(%22%5C%5Cs+%22,%22%22).split(%22,%22))%0A                .stream()%0A                .map(method -%3E HttpMethod.valueOf(method))%0A                .collect(Collectors.toSet());%0A    %7D
 %0A%7D%0A

add CORS to the UserInfo endpoint