|
1 | 1 | package com.stormpath.sdk.impl.http.authc; |
2 | 2 |
|
| 3 | +import com.stormpath.sdk.api.ApiKey; |
| 4 | +import com.stormpath.sdk.client.PairedApiKey; |
3 | 5 | import com.stormpath.sdk.impl.authc.credentials.ApiKeyCredentials; |
4 | 6 | import com.stormpath.sdk.impl.http.Request; |
5 | 7 | import com.stormpath.sdk.impl.http.support.RequestAuthenticationException; |
| 8 | +import com.stormpath.sdk.impl.util.Base64; |
6 | 9 | import com.stormpath.sdk.lang.Assert; |
7 | 10 |
|
| 11 | +import java.nio.charset.Charset; |
| 12 | + |
8 | 13 | public class SswsAuthenticator implements RequestAuthenticator { |
9 | 14 |
|
10 | 15 | public static final String AUTHENTICATION_SCHEME = "SSWS"; |
11 | 16 |
|
| 17 | + public static final Charset UTF8 = Charset.forName("UTF-8"); |
| 18 | + |
12 | 19 | private final ApiKeyCredentials apiKeyCredentials; |
| 20 | + private final PairedApiKey pairedApiKey; |
13 | 21 |
|
14 | 22 | public SswsAuthenticator(ApiKeyCredentials apiKeyCredentials) { |
15 | 23 | Assert.notNull(apiKeyCredentials, "apiKeyCredentials must be not be null."); |
16 | 24 | 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; |
17 | 31 | } |
18 | 32 |
|
19 | 33 | @Override |
20 | 34 | 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 | + |
21 | 51 | request.getHeaders().set(AUTHORIZATION_HEADER, AUTHENTICATION_SCHEME + " " + apiKeyCredentials.getSecret()); |
22 | 52 | } |
23 | 53 | } |
0 commit comments