Skip to content

Instantly share code, notes, and snippets.

@dougnoel
Last active December 8, 2022 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dougnoel/f75a08e46afaa4dec4a65fda92325926 to your computer and use it in GitHub Desktop.
Save dougnoel/f75a08e46afaa4dec4a65fda92325926 to your computer and use it in GitHub Desktop.
Authentication code removed from com.dougnoel.sentinel.apis.API.Java
private AuthenticationType authenticationType = NONE;
private Object authToken = null;
private static final AuthenticationType JWT = AuthenticationType.JWT;
private static final AuthenticationType AUTH_KEY = AuthenticationType.AUTH_KEY;
private static final AuthenticationType NONE = AuthenticationType.NONE;
/**
* Sets the authentication type that this API uses.
* @param authType com.dougnoel.sentinel.enums.AuthenticationType the type of authentication this API uses
*/
public void setAuthType(AuthenticationType authType) {
if(authType == JWT || authType == AUTH_KEY) {
authenticationType = authType;
}
}
/**
* Returns the authentication token.
* @return String the authentication token
*/
public String getAuthToken() {
String token = null;
if (authToken != null) {
token = authToken.toString();
}
return token;
}
/**
* Creates an authentication token based on the authentication type set. By capturing it from the currently open browser window.
*/
public void setAuthToken() {
switch (authenticationType) {
case JWT:
String jsExpression = "return JSON.parse(window.sessionStorage[Object.keys(window.sessionStorage).filter(key => /^oidc.*$/.test(key)).shift()]).id_token";
//return window.localStorage.getItem('id_token')").ToString()
authToken = ((JavascriptExecutor) WebDriverFactory.getWebDriver()).executeScript(jsExpression);
break;
case AUTH_KEY:
log.error("Auth Token capture is not yet implemented.");
break;
case NONE:
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment