Commit dd1b6fe5 authored by Matija Obreza's avatar Matija Obreza

Merge branch '8-support-password-grant' into 'master'

Resolve "Support password grant"

Closes #8

See merge request !8
parents da23386c 2d6c53c9
Pipeline #3035 passed with stage
in 27 seconds
......@@ -14,7 +14,9 @@ String scope = "write"; // write scope is required to manage data on Genesys
GenesysClient genesysClient = new GenesysClient(baseUrl, clientId, clientSecret, callbackUrl, scope);
```
### Authentication
### Authentication: Authorization code
For Genesys API clients that support `authorization_code` grant (such as websites, unsafe clients).
```java
// Authenticate the user
......@@ -32,10 +34,27 @@ genesysClient.me();
// genesysClient.getTokens().getRefreshToken();
```
### Authentication: Username and password
For Genesys API clients that support `password` grant (safe clients).
```java
// Obtain username and password
String username;
String password;
genesysClient.authenticate(username, password);
// Test it with /me
genesysClient.me();
// Tokens are now accessible. The refreshToken must be **safely** stored for future use.
// genesysClient.getTokens().getAccessToken();
// genesysClient.getTokens().getRefreshToken();
```
### Re-using tokens
If you have existing tokens you can provide the access and refresh tokens when
initializing the client:
If you have existing tokens you can provide the access and refresh tokens when initializing the client:
```java
GenesysTokens genesysTokens = new GenesysTokens();
......
......@@ -540,6 +540,24 @@ public class GenesysClient {
return query(Verb.POST, "/acn/" + instCode + "/delete", null, jsonAccessionIdList);
}
public void authenticate(String uname, String password) throws OAuthAuthenticationException {
try {
OAuth2AccessToken accessToken = service.getAccessTokenPasswordGrant(uname, password);
LOG.info("ACCESS TOKEN: {} scope={} raw={}", accessToken.getAccessToken(), accessToken.getScope(), accessToken.getRawResponse());
final String refreshToken = accessToken.getRefreshToken();
LOG.info("REFRESH TOKEN: {}", refreshToken);
tokens.setAccessToken(accessToken.getAccessToken());
tokens.setRefreshToken(refreshToken);
} catch (IOException | InterruptedException | ExecutionException e) {
LOG.error("Auth error", e);
throw new OAuthAuthenticationException(e.getMessage());
}
}
/**
* Obtain access and refresh tokens with verifier code.
*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment