-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(utils): extract the oauth token logic into utility method
- Loading branch information
Showing
2 changed files
with
62 additions
and
47 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/java/io/odh/test/platform/httpClient/OAuthToken.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,59 @@ | ||
/* | ||
* Copyright Skodjob authors. | ||
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). | ||
*/ | ||
package io.odh.test.platform.httpClient; | ||
|
||
import io.fabric8.openshift.api.model.OAuthAccessToken; | ||
import io.fabric8.openshift.api.model.OAuthAccessTokenBuilder; | ||
import io.fabric8.openshift.api.model.OAuthClient; | ||
import io.fabric8.openshift.api.model.OAuthClientBuilder; | ||
import io.fabric8.openshift.api.model.User; | ||
import io.skodjob.testframe.resources.KubeResourceManager; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Base64; | ||
import java.util.Random; | ||
|
||
public class OAuthToken { | ||
public String getToken(String redirectUrl) throws NoSuchAlgorithmException { | ||
// https://github.com/openshift/cluster-authentication-operator/blob/master/test/library/client.go#L35-L44 | ||
MessageDigest digest = MessageDigest.getInstance("SHA-256"); | ||
String sha256Prefix = "sha256~"; | ||
String randomToken = "nottoorandom%d".formatted(new Random().nextInt()); | ||
byte[] hashed = digest.digest(randomToken.getBytes(StandardCharsets.UTF_8)); | ||
String privateToken = sha256Prefix + randomToken; | ||
String publicToken = sha256Prefix + Base64.getUrlEncoder().withoutPadding().encodeToString(hashed); | ||
|
||
User user = KubeResourceManager.getKubeClient().getOpenShiftClient().users().withName("kubeadmin").get(); | ||
|
||
final String oauthClientName = "oauth-client"; | ||
OAuthClient client = new OAuthClientBuilder() | ||
.withNewMetadata() | ||
.withName(oauthClientName) | ||
.endMetadata() | ||
.withSecret("the-secret-for-oauth-client") | ||
.withRedirectURIs("https://localhost") | ||
.withGrantMethod("auto") | ||
.withAccessTokenInactivityTimeoutSeconds(300) | ||
.build(); | ||
KubeResourceManager.getInstance().createResourceWithoutWait(client); | ||
|
||
OAuthAccessToken token = new OAuthAccessTokenBuilder() | ||
.withNewMetadata() | ||
.withName(publicToken) | ||
.endMetadata() | ||
.withExpiresIn(86400L) | ||
.withScopes("user:full") | ||
.withRedirectURI(redirectUrl) | ||
.withClientName(oauthClientName) | ||
.withUserName(user.getMetadata().getName()) | ||
.withUserUID(user.getMetadata().getUid()) | ||
.build(); | ||
KubeResourceManager.getInstance().createResourceWithWait(token); | ||
|
||
return privateToken; | ||
} | ||
} |
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