Adapter: OAuth2

AeroGear.Authorization.adapters. OAuth2

new OAuth2(name, settings={}) → {Object}

Status: Experimental
The OAuth2 adapter is the default type used when creating a new authorization module. It uses AeroGear.ajax to communicate with the server. This constructor is instantiated when the "Authorizer.add()" method is called
Parameters:
Name Type Argument Description
name String the name used to reference this particular authz module
settings={} Object the settings to be passed to the adapter
settings.clientId String the client id/ app Id of the protected service
settings.redirectURL String the URL to redirect to
settings.authEndpoint String the endpoint for authorization
settings.validationEndpoint String <optional>
the optional endpoint to validate your token. Not in the Spec, but recommend for use with Google's API's
settings.scopes String a space separated list of "scopes" or things you want to access
Source:
Returns:
The created authz module
Type
Object
Example
    // Create an empty Authenticator
    var authz = AeroGear.Authorization();

    authz.add({
        name: "coolThing",
        settings: {
            clientId: "12345",
            redirectURL: "http://localhost:3000/redirector.html",
            authEndpoint: "http://localhost:3000/v1/authz",
            scopes: "userinfo coolstuff"
        }
    });

Methods

execute(options={}) → {Object}

Parameters:
Name Type Argument Default Description
options={} Object Options to pass to the execute method
options.type String <optional>
"GET" the type of the request
options.url String <optional>
the url of the secured endpoint you want to access
Source:
Returns:
The ES6 promise (exposes AeroGear.ajax response as a response parameter; if an error is returned, the authentication URL will be appended to the response object)
Type
Object
Example
    // Create the Authorizer
    var authz = AeroGear.Authorization();

    authz.add({
        name: "coolThing",
        settings: {
            clientId: "12345",
            redirectURL: "http://localhost:3000/redirector.html",
            authEndpoint: "http://localhost:3000/v1/authz",
            scopes: "userinfo coolstuff"
        }
    });


    // Make the authorization call.
    authz.services.coolThing.execute()
        .then( function( response ){
            ...
        })
        .catch( function( error ) {
            ...
        });

validate(queryString) → {Object}

Validate the Authorization endpoints - Takes the querystring that is returned after the "dance" unparsed.
Parameters:
Name Type Description
queryString String The returned query string to be parsed
Source:
Returns:
The ES6 promise (exposes AeroGear.ajax response as a response parameter; if an error is returned, the authentication URL will be appended to the response object)
Type
Object
Example
    // Create the Authorizer
    var authz = AeroGear.Authorization();

    authz.add({
        name: "coolThing",
        settings: {
            clientId: "12345",
            redirectURL: "http://localhost:3000/redirector.html",
            authEndpoint: "http://localhost:3000/v1/authz",
            scopes: "userinfo coolstuff"
        }
    });

    // Make the call.
    authz.services.coolThing.execute({url: "http://localhost:3000/v1/authz/endpoint", type: "GET"})
        .then( function( response ){
            ...
        })
        .catch( function( error ) {
            // an error happened, so take the authURL and do the "OAuth2 Dance",
        });
    });

    // After a successful response from the "OAuth2 Dance", validate that the query string is valid, If all is well, the access_token will be stored.
    authz.services.coolThing.validate( responseFromAuthEndpoint )
        .then( function( response ){
            ...
        })
        .catch( function( error ) {
            ...
        });