package oasis.openidconnect;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyPair;

import org.joda.time.Duration;

import com.google.inject.AbstractModule;
import com.typesafe.config.Config;

import oasis.security.KeyPairLoader;

public class OpenIdConnectModule extends AbstractModule {
  public static class Settings {

    public static Builder builder() {
      return new Builder();
    }

    public static Settings fromConfig(Config config) {
      // TODO: refactor to use a single subtree of the config
      Path privateKeyPath = null;
      Path publicKeyPath = null;

      if (config.hasPath("oasis.conf-dir")) {
        Path confDir = Paths.get(config.getString("oasis.conf-dir"));
        privateKeyPath = confDir.resolve(config.getString("oasis.openid-connect.private-key-path"));
        publicKeyPath = confDir.resolve(config.getString("oasis.openid-connect.public-key-path"));
      }

      return Settings.builder()
          .setKeyPair(KeyPairLoader.loadOrGenerateKeyPair(privateKeyPath, publicKeyPath))
          .setAccessTokenDuration(Duration.millis(config.getMilliseconds("oasis.oauth.access-token-duration")))
          .setIdTokenDuration(Duration.millis(config.getMilliseconds("oasis.openid-connect.id-token-duration")))
          .build();
    }

    public static class Builder {

      private KeyPair keyPair;
      private Duration accessTokenDuration;
      private Duration idTokenDuration;

      public Settings build() {
        return new Settings(this);
      }

      public Builder setKeyPair(KeyPair keyPair) {
        this.keyPair = keyPair;
        return this;
      }

      public Builder setAccessTokenDuration(Duration accessTokenDuration) {
        this.accessTokenDuration = accessTokenDuration;
        return this;
      }

      public Builder setIdTokenDuration(Duration idTokenDuration) {
        this.idTokenDuration = idTokenDuration;
        return this;
      }
    }

    public final KeyPair keyPair;
    public final Duration accessTokenDuration;
    public final Duration idTokenDuration;

    private Settings(Builder builder) {
      this.keyPair = builder.keyPair;
      this.accessTokenDuration = builder.accessTokenDuration;
      this.idTokenDuration = builder.idTokenDuration;
    }
  }

  public static OpenIdConnectModule create(Config config) {
    return new OpenIdConnectModule(Settings.fromConfig(config));
  }

  private final Settings settings;

  public OpenIdConnectModule(Settings settings) {
    this.settings = settings;
  }

  @Override
  protected void configure() {
    bind(Settings.class).toInstance(settings);
  }
}

@@ -107,16 +107,54 @@
 KeyPair;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Aimport
@@ -1147,36 +1147,32 @@
 s(config.get
-Milliseconds
+Duration
 (%22oasis.oaut
@@ -1187,32 +1187,55 @@
 -token-duration%22
+, TimeUnit.MILLISECONDS
 )))%0A          .s
@@ -1282,20 +1282,16 @@
 .get
-Milliseconds
+Duration
 (%22oa
@@ -1327,16 +1327,39 @@
 uration%22
+, TimeUnit.MILLISECONDS
 )))%0A    

Use com.typesafe.config.Config#getDuration instead of getMilliseconds