-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AuthorizationProvider to ClientOptions and WorkflowService [2] (#632
- Loading branch information
1 parent
8c61213
commit 4ba3020
Showing
6 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/uber/cadence/serviceclient/auth/AdminJwtAuthorizationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Modifications Copyright (c) 2017-2021 Uber Technologies Inc. | ||
* Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc. | ||
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
* use this file except in compliance with the License. A copy of the License is | ||
* located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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.uber.cadence.serviceclient.auth; | ||
|
||
import com.auth0.jwt.JWT; | ||
import com.auth0.jwt.JWTCreator; | ||
import com.auth0.jwt.algorithms.Algorithm; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.interfaces.RSAPrivateKey; | ||
import java.security.interfaces.RSAPublicKey; | ||
import java.sql.Date; | ||
import java.time.Clock; | ||
|
||
public class AdminJwtAuthorizationProvider implements IAuthorizationProvider { | ||
|
||
private final RSAPrivateKey rsaPrivateKey; | ||
private final RSAPublicKey rsaPublicKey; | ||
|
||
public AdminJwtAuthorizationProvider(RSAPublicKey publicKey, RSAPrivateKey privateKey) { | ||
this.rsaPrivateKey = privateKey; | ||
this.rsaPublicKey = publicKey; | ||
} | ||
|
||
@Override | ||
public byte[] getAuthToken() { | ||
final JWTCreator.Builder jwtBuilder = JWT.create(); | ||
jwtBuilder.withClaim("admin", true); | ||
jwtBuilder.withClaim("ttl", 60 * 10); | ||
jwtBuilder.withIssuedAt(Date.from(Clock.systemUTC().instant())); | ||
return jwtBuilder | ||
.sign(Algorithm.RSA256(this.rsaPublicKey, this.rsaPrivateKey)) | ||
.getBytes(StandardCharsets.UTF_8); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/uber/cadence/serviceclient/auth/IAuthorizationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Modifications Copyright (c) 2017-2021 Uber Technologies Inc. | ||
* Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc. | ||
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
* use this file except in compliance with the License. A copy of the License is | ||
* located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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.uber.cadence.serviceclient.auth; | ||
|
||
public interface IAuthorizationProvider { | ||
// getAuthToken provides the OAuth authorization token | ||
// It's called before every request to Cadence server, and sets the token in the request header. | ||
byte[] getAuthToken(); | ||
} |
82 changes: 82 additions & 0 deletions
82
src/test/java/com/uber/cadence/serviceclient/auth/AdminJwtAuthorizationProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.uber.cadence.serviceclient.auth; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import com.auth0.jwt.JWT; | ||
import com.auth0.jwt.interfaces.Claim; | ||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.KeyFactory; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.interfaces.RSAPrivateKey; | ||
import java.security.interfaces.RSAPublicKey; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.PKCS8EncodedKeySpec; | ||
import java.security.spec.X509EncodedKeySpec; | ||
import org.apache.commons.codec.binary.Base64; | ||
import org.junit.Test; | ||
|
||
public class AdminJwtAuthorizationProviderTest { | ||
@Test | ||
public void testCreateAuthToken() throws NoSuchAlgorithmException, InvalidKeySpecException { | ||
|
||
Base64 b64 = new Base64(); | ||
byte[] decodedPub = b64.decode(testPublicKey.getBytes(StandardCharsets.UTF_8)); | ||
byte[] decodedPri = b64.decode(testPrivateKey.getBytes(StandardCharsets.UTF_8)); | ||
|
||
KeyFactory rsaKeyFactory = KeyFactory.getInstance("RSA"); | ||
|
||
final RSAPublicKey rsaPublicKey = | ||
(RSAPublicKey) rsaKeyFactory.generatePublic(new X509EncodedKeySpec(decodedPub)); | ||
|
||
final RSAPrivateKey rsaPrivateKey = | ||
(RSAPrivateKey) rsaKeyFactory.generatePrivate(new PKCS8EncodedKeySpec(decodedPri)); | ||
|
||
final AdminJwtAuthorizationProvider authProvider = | ||
new AdminJwtAuthorizationProvider(rsaPublicKey, rsaPrivateKey); | ||
final String jwt = new String(authProvider.getAuthToken(), StandardCharsets.UTF_8); | ||
|
||
final DecodedJWT decodedJwt = JWT.decode(jwt); | ||
final Claim adminClaim = decodedJwt.getClaim("admin"); | ||
assertTrue(adminClaim.asBoolean()); | ||
final Claim ttlClaim = decodedJwt.getClaim("ttl"); | ||
assertEquals((int) (60 * 10), (int) ttlClaim.asInt()); | ||
} | ||
|
||
private static String testPublicKey = | ||
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAscukltHilaq+o5gIVE4P\n" | ||
+ "GwWl+esvJ2EaEpWw6ogr98Un11YJ4oKkwIkLw4iIo0tveCINA3cZmxaW1RejRWKE\n" | ||
+ "qYFtQ1rYd6BsnFAHXWh2R3A1FtpG6ANUEGkE7OAJe2/L42E/ImJ+GQxRvartInDM\n" | ||
+ "yfiRfB7+L2n3wG+Ni+hBNMtAaX4Wwbj2hup21Jjuo96TuhcGImBFBATGWaYR2wqe\n" | ||
+ "/6by9wJexPHlY/1uDp3SnzF1dCLjp76SGCfyYqOGC/PxhQi7mDxeH9/tIC+lt/Sz\n" | ||
+ "wc1n8gZLtlRlZHinvYa8lhWXqVYw6WD8h4LTgALq9iY+beD1PFQSY1GkQtt0RhRw\n" | ||
+ "eQIDAQAB"; | ||
|
||
private static String testPrivateKey = | ||
"MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCxy6SW0eKVqr6j\n" | ||
+ "mAhUTg8bBaX56y8nYRoSlbDqiCv3xSfXVgnigqTAiQvDiIijS294Ig0DdxmbFpbV\n" | ||
+ "F6NFYoSpgW1DWth3oGycUAddaHZHcDUW2kboA1QQaQTs4Al7b8vjYT8iYn4ZDFG9\n" | ||
+ "qu0icMzJ+JF8Hv4vaffAb42L6EE0y0BpfhbBuPaG6nbUmO6j3pO6FwYiYEUEBMZZ\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