View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2015-2017 ForgeRock AS.
15   */
16  
17  package org.forgerock.selfservice.example;
18  
19  import java.security.KeyPairGenerator;
20  import java.security.NoSuchAlgorithmException;
21  
22  import org.wrensecurity.guava.common.base.Optional;
23  import org.forgerock.json.jose.jws.SigningManager;
24  import org.forgerock.json.jose.jws.handlers.SigningHandler;
25  import org.forgerock.selfservice.core.snapshot.SnapshotTokenConfig;
26  import org.forgerock.tokenhandler.TokenHandler;
27  import org.forgerock.selfservice.core.snapshot.SnapshotTokenHandlerFactory;
28  import org.forgerock.json.jose.tokenhandler.JwtTokenHandler;
29  import org.forgerock.selfservice.stages.tokenhandlers.JwtTokenHandlerConfig;
30  
31  /**
32   * Basic token handler factory that always returns the same handler.
33   *
34   * @since 0.1.0
35   */
36  final class ExampleTokenHandlerFactory implements SnapshotTokenHandlerFactory {
37  
38      @Override
39      public TokenHandler get(SnapshotTokenConfig snapshotTokenConfig) {
40          switch (snapshotTokenConfig.getType()) {
41          case JwtTokenHandlerConfig.TYPE:
42              return createJwtTokenHandler((JwtTokenHandlerConfig) snapshotTokenConfig);
43          default:
44              throw new IllegalArgumentException("Unknown type " + snapshotTokenConfig.getType());
45          }
46      }
47  
48      private TokenHandler createJwtTokenHandler(JwtTokenHandlerConfig config) {
49          try {
50              SigningManager signingManager = new SigningManager();
51              SigningHandler signingHandler = signingManager.newHmacSigningHandler(config.getSharedKey());
52  
53              KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(config.getKeyPairAlgorithm());
54              keyPairGen.initialize(config.getKeyPairSize());
55  
56              return new JwtTokenHandler(
57                      config.getJweAlgorithm(),
58                      config.getEncryptionMethod(),
59                      keyPairGen.generateKeyPair(),
60                      config.getJwsAlgorithm(),
61                      signingHandler,
62                      Optional.of(config.getTokenLifeTimeInSeconds()));
63  
64          } catch (NoSuchAlgorithmException nsaE) {
65              throw new RuntimeException("Unable to create key pair for encryption", nsaE);
66          }
67      }
68  
69  }