Skip to content

Instantly share code, notes, and snippets.

@mrioan
Created October 14, 2014 20:24
Show Gist options
  • Save mrioan/ac55e204de4f3805fee6 to your computer and use it in GitHub Desktop.
Save mrioan/ac55e204de4f3805fee6 to your computer and use it in GitHub Desktop.
EXCHANGING API KEYS FOR OAUTH 2.0 TOKENS
import com.stormpath.sdk.api.ApiKey;
import com.stormpath.sdk.api.ApiKeys;
import com.stormpath.sdk.application.Application;
import com.stormpath.sdk.client.Client;
import com.stormpath.sdk.client.Clients;
import com.stormpath.sdk.error.authc.AccessTokenOauthException;
import com.stormpath.sdk.http.HttpMethod;
import com.stormpath.sdk.http.HttpRequest;
import com.stormpath.sdk.http.HttpRequests;
import com.stormpath.sdk.impl.util.Base64;
import com.stormpath.sdk.oauth.AccessTokenResult;
import com.stormpath.sdk.oauth.OauthAuthenticationResult;
import org.junit.Test;
import java.util.LinkedHashMap;
import java.util.Map;
public class ApiKeys_Oauth20 {
String applicationUrl = "https://api.stormpath.com/v1/applications/3TtbyZ2qo84eQM4lTo2H92";
String path = System.getProperty("user.home") + "/.stormpath/apiKey.properties";
Client client = Clients.builder().setApiKey(ApiKeys.builder().setFileLocation(path).build()).build();
Application application = client.getResource(applicationUrl, Application.class);
@Test
public void executeSomeOauth2AuthenticatedOperation() {
String userApiKeyPath = System.getProperty("user.home") + "/.stormpath/user_apiKey.properties";
ApiKey userApiKey = ApiKeys.builder().setFileLocation(userApiKeyPath).build();
//Developer requests access token
String accessToken = getAccessToken(userApiKey);
//Developer executes an authenticated operation with the provided accessToken
if (executeOperationX(accessToken)) {
System.out.print("Execution allowed");
} else {
System.out.print("Execution denied");
}
}
public String getAccessToken(ApiKey apiKey) {
HttpRequest request = createOauthAuthenticationRequest(apiKey);
AccessTokenResult accessTokenResult = (AccessTokenResult) application.authenticateApiRequest(request);
return accessTokenResult.getTokenResponse().getAccessToken();
}
public boolean executeOperationX(String accessToken) {
HttpRequest request = createRequestForOauth2AuthenticatedOperation(accessToken);
try {
OauthAuthenticationResult result = application.authenticateOauthRequest(request).execute();
System.out.println(result.getApiKey());
System.out.println(result.getAccount());
return true;
} catch (AccessTokenOauthException e) {
return false;
}
}
private HttpRequest createOauthAuthenticationRequest(ApiKey apiKey) {
try {
String credentials = apiKey.getId() + ":" + apiKey.getSecret();
Map<String, String[]> headers = new LinkedHashMap<String, String[]>();
headers.put("Accept", new String[]{"application/json"});
headers.put("Content-Type", new String[]{"application/x-www-form-urlencoded"});
headers.put("Authorization", new String[]{"Basic " + Base64.encodeBase64String(credentials.getBytes("UTF-8"))});
Map<String, String[]> parameters = new LinkedHashMap<String, String[]>();
parameters.put("grant_type", new String[]{"client_credentials"});
HttpRequest request = HttpRequests.method(HttpMethod.POST)
.headers(headers)
.parameters(parameters)
.build();
return request;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private HttpRequest createRequestForOauth2AuthenticatedOperation(String token) {
try {
Map<String, String[]> headers = new LinkedHashMap<String, String[]>();
headers.put("Accept", new String[]{"application/json"});
headers.put("Authorization", new String[]{"Bearer " + token});
HttpRequest request = HttpRequests.method(HttpMethod.GET)
.headers(headers)
.build();
return request;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment