/**
 * Ozwillo Kernel
 * Copyright (C) 2015  Atol Conseils & Développements
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package oasis.web.openidconnect;

import java.net.URI;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import com.fasterxml.jackson.annotation.JsonProperty;

import oasis.model.authz.Scopes;
import oasis.urls.Urls;
import oasis.web.authn.LogoutPage;
import oasis.web.authz.AuthorizationEndpoint;
import oasis.web.authz.KeysEndpoint;
import oasis.web.authz.RevokeEndpoint;
import oasis.web.authz.TokenEndpoint;
import oasis.web.resteasy.Resteasy1099;
import oasis.web.userinfo.UserInfoEndpoint;

/**
 * See <a href="http://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig">OpenID Connect Discovery 1.0</a>
 */
@Path("/.well-known/openid-configuration")
public class OpenIdProviderConfigurationEndpoint {
  @Inject Urls urls;

  @Context UriInfo uriInfo;

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Configuration get() {
    return new Configuration();
  }

  private URI getBaseUri() {
    if (urls.canonicalBaseUri().isPresent()) {
      return urls.canonicalBaseUri().get();
    }
    return Resteasy1099.getBaseUri(uriInfo);
  }

  private UriBuilder getBaseUriBuilder() {
    if (urls.canonicalBaseUri().isPresent()) {
      return UriBuilder.fromUri(urls.canonicalBaseUri().get());
    }
    return Resteasy1099.getBaseUriBuilder(uriInfo);
  }

  /**
   * See <a href="http://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata">OpenId Provider Metadata</a>
   */
  public class Configuration {
    @JsonProperty String issuer = getBaseUri().toString();
    @JsonProperty String authorization_endpoint = getBaseUriBuilder().path(AuthorizationEndpoint.class).build().toString();
    @JsonProperty String token_endpoint = getBaseUriBuilder().path(TokenEndpoint.class).build().toString();
    @JsonProperty String userinfo_endpoint = getBaseUriBuilder().path(UserInfoEndpoint.class).build().toString();
    @JsonProperty String jwks_uri = getBaseUriBuilder().path(KeysEndpoint.class).build().toString();
    // registration_endpoint
    @JsonProperty String[] scopes_supported = { Scopes.OPENID, Scopes.PROFILE, Scopes.EMAIL, Scopes.ADDRESS, Scopes.PHONE, Scopes.OFFLINE_ACCESS };
    /** See {@link AuthorizationEndpoint#validateResponseTypeAndMode}. */
    @JsonProperty String[] response_types_supported = { "code" };
    /** See {@link AuthorizationEndpoint#generateAuthorizationCodeAndRedirect}. */
    @JsonProperty String[] response_modes_supported = { "query" };
    /** See {@link TokenEndpoint#validate}. */
    @JsonProperty String[] grant_types_supported = { "authorization_code", "refresh_token" };
    // acr_values_supported
    // TODO: support "pairwise" subject types? How to correlate them in DataCore and other Data Providers then?
    @JsonProperty String[] subject_types_supported = { "public" };
    /** See {@link AuthorizationEndpoint} and {@link TokenEndpoint}. */
    @JsonProperty String[] id_token_signing_alg_values_supported = { "RS256" };
    // id_token_encryption_alg_supported, id_token_encryption_enc_values_supported
    /** See {@link UserInfoEndpoint}. */
    @JsonProperty String[] userinfo_signing_alg_values_supported = { "RS256" };
    // userinfo_encryption_alg_values_supported, userinfo_encryption_enc_values_supported
    // request_object_signing_alg_values_supported, request_object_encryption_alg_values_supported, request_object_encryption_enc_values_supported
    /** See {@link oasis.web.authn.ClientAuthenticationFilter}. */
    @JsonProperty String[] token_endpoint_auth_methods_supported = { "client_secret_basic" };
    // token_endpoint_auth_signing_alg_values_supported
    // display_values_supported, claim_types_supported, claims_supported
    // TODO: service_documentation
    // claims_locales_supported
    // TODO: ui_locales_supported
    // This is the default value: @JsonProperty boolean claims_parameter_supported = false;
    // This is the default value: @JsonProperty boolean request_parameter_supported = false;
    @JsonProperty boolean request_uri_parameter_supported = false;
    // This is the default value: @JsonProperty boolean require_request_uri_registration = false;
    // TODO: op_policy_uri, op_tos_uri

    // See http://openid.net/specs/openid-connect-session-1_0.html#EndpointDiscovery
    // TODO: check_session_iframe
    @JsonProperty String end_session_endpoint = getBaseUriBuilder().path(LogoutPage.class).build().toString();

    // See http://lists.openid.net/pipermail/openid-specs-ab/Week-of-Mon-20140120/004581.html
    @JsonProperty String revocation_endpoint = getBaseUriBuilder().path(RevokeEndpoint.class).build().toString();
  }
}

@@ -1096,16 +1096,138 @@
 roperty;
+%0Aimport com.google.common.base.Function;%0Aimport com.google.common.collect.FluentIterable;%0Aimport com.ibm.icu.util.ULocale;
 %0A%0Aimport
@@ -1464,32 +1464,68 @@
 .TokenEndpoint;%0A
+import oasis.web.i18n.LocaleHelper;%0A
 import oasis.web
@@ -1767,16 +1767,16 @@
 ation%22)%0A
-
 public c
@@ -1818,16 +1818,327 @@
 point %7B%0A
+  private static final String%5B%5D UI_LOCALES_SUPPORTED = FluentIterable.from(LocaleHelper.SUPPORTED_LOCALES)%0A      .transform(new Function%3CULocale, String%3E() %7B%0A        @Override%0A        public String apply(ULocale input) %7B%0A          return input.toLanguageTag();%0A        %7D%0A      %7D)%0A      .toArray(String.class);%0A%0A
   @Injec
@@ -5133,37 +5133,75 @@
     
-// TODO: ui_locales_supported
+@JsonProperty String%5B%5D ui_locales_supported = UI_LOCALES_SUPPORTED;
 %0A   

Add ui_locales_supported to .well-known/openid-configuration