Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit 9c17dc2

Browse files
committed
1309: added /oauth2/ client creds support
1 parent 1c58af2 commit 9c17dc2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,53 @@
11
package com.stormpath.sdk.impl.http.authc;
22

3+
import com.stormpath.sdk.api.ApiKey;
4+
import com.stormpath.sdk.client.PairedApiKey;
35
import com.stormpath.sdk.impl.authc.credentials.ApiKeyCredentials;
46
import com.stormpath.sdk.impl.http.Request;
57
import com.stormpath.sdk.impl.http.support.RequestAuthenticationException;
8+
import com.stormpath.sdk.impl.util.Base64;
69
import com.stormpath.sdk.lang.Assert;
710

11+
import java.nio.charset.Charset;
12+
813
public class SswsAuthenticator implements RequestAuthenticator {
914

1015
public static final String AUTHENTICATION_SCHEME = "SSWS";
1116

17+
public static final Charset UTF8 = Charset.forName("UTF-8");
18+
1219
private final ApiKeyCredentials apiKeyCredentials;
20+
private final PairedApiKey pairedApiKey;
1321

1422
public SswsAuthenticator(ApiKeyCredentials apiKeyCredentials) {
1523
Assert.notNull(apiKeyCredentials, "apiKeyCredentials must be not be null.");
1624
this.apiKeyCredentials = apiKeyCredentials;
25+
26+
ApiKey apiKey = apiKeyCredentials.getApiKey();
27+
Assert.notNull(apiKey, "apiKeyCredentials argument cannot have a null apiKey");
28+
29+
Assert.isInstanceOf(PairedApiKey.class, apiKey, "apiKeyCredentials.getApiKey() must be a PairedApiKey instance");
30+
this.pairedApiKey = (PairedApiKey) apiKey;
1731
}
1832

1933
@Override
2034
public void authenticate(Request request) throws RequestAuthenticationException {
35+
36+
if (request.getResourceUrl().getPath().startsWith("/oauth2/")) {
37+
38+
ApiKey oauthClientCreds = this.pairedApiKey.getSecondaryApiKey();
39+
Assert.notNull(oauthClientCreds, "PairedApiKey credentials must have a secondary api key when invoking an Okta /oauth2/ endpoint.");
40+
41+
String basicCreds = oauthClientCreds.getId() + ":" + oauthClientCreds.getSecret();
42+
String base64 = Base64.encodeBase64String(basicCreds.getBytes(UTF8));
43+
44+
45+
request.getHeaders().set(AUTHORIZATION_HEADER, "BASIC " + base64);
46+
47+
return;
48+
}
49+
50+
2151
request.getHeaders().set(AUTHORIZATION_HEADER, AUTHENTICATION_SCHEME + " " + apiKeyCredentials.getSecret());
2252
}
2353
}

0 commit comments

Comments
 (0)