// Copyright (C) 2015 The Android Open Source Project
//
// 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 com.googlesource.gerrit.plugins.cfoauth;

import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.pgm.init.api.ConsoleUI;
import com.google.gerrit.pgm.init.api.InitStep;
import com.google.gerrit.pgm.init.api.Section;
import com.google.inject.Inject;

class InitOAuthConfig implements InitStep {
  static final String PLUGIN_SECTION = "plugin";
  static final String SERVER_URL = "serverUrl";
  static final String CLIENT_ID = "clientId";
  static final String CLIENT_SECRET = "clientSecret";
  static final String VERIFIY_SIGNATURES = "verifySignatures";

  static final String DEFAULT_SERVER_URL = "http://localhost:8080/uaa";
  static final String DEFAULT_CLIENT_ID = "gerrit";

  private final ConsoleUI ui;
  private final Section cfg;

  @Inject
  InitOAuthConfig(ConsoleUI ui,
      Section.Factory sections,
      @PluginName String pluginName) {
    this.ui = ui;
    this.cfg = sections.get(PLUGIN_SECTION, pluginName);
  }

  @Override
  public void run() throws Exception {
    ui.header("Cloud Foundry UAA OAuth 2.0 Authentication Provider");
    cfg.string("UAA server URL", SERVER_URL, DEFAULT_SERVER_URL);
    cfg.string("Client id", CLIENT_ID, DEFAULT_CLIENT_ID);
    cfg.passwordForKey("Client secret", CLIENT_SECRET);
    cfg.set(VERIFIY_SIGNATURES, Boolean.toString(
        ui.yesno(true, "Verify token signatures", VERIFIY_SIGNATURES)));
  }

  @Override
  public void postRun() throws Exception {
  }
}

@@ -652,16 +652,59 @@
 oauth;%0A%0A
+import com.google.common.base.CharMatcher;%0A
 import c
@@ -804,16 +804,65 @@
 soleUI;%0A
+import com.google.gerrit.pgm.init.api.InitFlags;%0A
 import c
@@ -1412,16 +1412,49 @@
 rrit%22;%0A%0A
+  private final InitFlags flags;%0A
   privat
@@ -1533,16 +1533,33 @@
 hConfig(
+InitFlags flags, 
 ConsoleU
@@ -1635,16 +1635,40 @@
 Name) %7B%0A
+    this.flags = flags;%0A
     this
@@ -2164,16 +2164,151 @@
 RES)));%0A
+    flags.cfg.setString(%22auth%22, null, %22logouturl%22, CharMatcher.is('/')%0A        .trimTrailingFrom(cfg.get(SERVER_URL)) + %22/logout.do%22);%0A
   %7D%0A%0A  @

Set the auth.logouturl property during init