Implicit Flow

The implicit grant type is used to obtain access tokens (it does not support the issuance of refresh tokens) and is optimized for public clients known to operate a particular redirection URI. These clients are typically implemented in a browser using a scripting language such as JavaScript.

More Details https://tools.ietf.org/html/rfc6749#section-4.2

How to Use in Java

1) Create App in Cidaas

To work with implicit flow we need to create Browser based application in cidaas app section

2) Get Access Token

In this example I am going to use apache oltu library. for more library please visit https://oauth.net/code/

        String appID = "your app id";
        String secret = "your secret";

        OAuthClientRequest clientReq = OAuthClientRequest
                .authorizationLocation("yourcidaasdomain/authz-srv/authz")
                .setClientId(appID).setRedirectURI("http://localhost:8080")
                .setResponseType(ResponseType.TOKEN.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 hash of access_token, expire_in

Example:

http://localhost:8080#access_token=eyJ...&expires_in=86400&viewtype=login

3) 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": ""
    }
}



results matching ""

    No results matching ""