Authorization Code Flow
The authorization code grant type is used to obtain both access tokens and refresh tokens and is optimized for confidential clients. Since this is a redirection-based flow, the client must be capable of interacting with the resource owner's user-agent (typically a web browser) and capable of receiving incoming requests (via redirection) from the authorization server.
More info https://tools.ietf.org/html/rfc6749#section-4.1
How to Use in Java
1) Create App in Cidaas
To work with Authorization code flow we need to create regular web application
in cidaas app section
2) Get the User's Authorization and authorization code
In this example I am going to use apache oltu library
. for more library please visit https://oauth.net/code/
String appID = "your client";
String secret = "your secret";
OAuthClientRequest clientReq = OAuthClientRequest
.authorizationLocation("yourcidaasdomain/authz-srv/authz")
.setClientId(appID).setRedirectURI("http://localhost:8080")
.setResponseType(ResponseType.CODE.toString()).buildQueryMessage();
String authURL = clientReq.getLocationUri();
System.out.println(authURL);
This code will give you the autherization url. redirect to the browser with this URL. Once the User logged in to the account , cidaas will redirect to the redirect_uri with the query string of code
Example:
3) Get Access Token
OAuthClientRequest clientReqAccessToken = OAuthClientRequest
.tokenLocation("yourcidaasdomain/token-srv/token")
.setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(appID).setClientSecret(appsecret)
.setRedirectURI("http://localhost:8080").setCode(code).buildBodyMessage();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthAccessTokenResponse oAuthResponse = oAuthClient.accessToken(clientReqAccessToken);
System.out.println(
"Access Token: " + oAuthResponse.getAccessToken() + ", Expires in: " + oAuthResponse.getBody());
4) Get User info
Once you got the access_token pass the access_token to cidaas user info url.
try {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("access_token", access_token);
String resourceResponse = submitApi("yourcidaasdomain/users-srv/userinfo",
headers);
System.out.println(resourceResponse);
} catch (Exception e) {
System.out.println("Error while getting userinfo , Error : " + e.getMessage());
}
Http Call
public static String submitApi(String uri, HashMap<String, String> headers) {
HttpGet httpMethod = new HttpGet(uri);
if (headers.size() > 0) {
for (String key : headers.keySet()) {
httpMethod.addHeader(key, headers.get(key));
}
}
try {
HttpResponse response = HttpClientBuilder.create().build().execute(httpMethod);
int responseStatusCode = response.getStatusLine().getStatusCode();
if (responseStatusCode == HttpStatus.SC_OK) {
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer resultData = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
resultData.append(line);
}
return resultData.toString();
} catch (Exception ex) {
httpMethod.abort();
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return "";
}
User info format
{
"_id": "b9e003ab-8a1e-4540-bf52-0440a904efbe",
"updatedTime": "2018-06-13T14:25:34.035Z",
"createdTime": "2018-06-13T14:25:34.035Z",
"className": "de.cidaas.core.db.SocialIdentity",
"password_hash": "$2b$13$xe/cdOLuf8zwmRanWkmiTu7PjaesTEykgeBukGH4EzH/l0hKopIae",
"mobile_number_verified": false,
"mobile_number_obj": {
"updatedTime": "2018-06-13T14:25:34.034Z",
"createdTime": "2018-06-13T14:25:34.034Z",
"className": "de.cidaas.core.db.MobileEntity",
"_id": "295c49c6-ee0b-48ea-9767-6985ae66237a",
"carrier_name": "Vodafone Essar South Ltd",
"carrier_type": "mobile",
"country": "IN",
"dail_code": "91",
"E164_format": "+919643435147",
"given_phone": "+919643435147",
"international_format": "+91 96434 35147",
"national_format": "096434 35147",
"phone": "9643435147"
},
"mobile_number": "+919643435147",
"locale": "en-us",
"given_name": "William",
"family_name": "jonass",
"email_verified": true,
"email": "williamjonass@gmail.com",
"provider": "self",
"sub": "b2f0bf7d-d060-4014-a90c-f3d9b6b8ade3",
"__v": 0,
"roles": [
"USER"
],
"groups": [
{
"_id": "f12dac45-78e7-4d27-93d2-c09435667ef4",
"sub": "b2f0bf7d-d060-4014-a90c-f3d9b6b8ade3",
"groupId": "CIDAAS_ADMINS",
"path": "/CIDAAS_ADMINS/",
"roles": []
}
],
"name": "William Jonass",
"preferred_username": "williamjonass@gmail.com",
"nickname": "william",
"updated_at": 1528899934,
"identities": [
{
"provider": "self",
"identityId": "b9e003ab-8a1e-4540-bf52-0440a904efbe",
"email": "williamjonass@gmail.com",
"mobile_number": "+919643435147"
}
],
"customFields": {
"Hobbies": [],
"customfield1": ""
}
}