@carllewi2/sapcdc-ally-driver
Version:
A custom ally driver for SAPCDC
156 lines (155 loc) • 5.7 kB
JavaScript
"use strict";
/*
|--------------------------------------------------------------------------
| Ally Oauth driver
|--------------------------------------------------------------------------
|
| This is a dummy implementation of the Oauth driver. Make sure you
|
| - Got through every line of code
| - Read every comment
|
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SapcdcDriver = void 0;
const standalone_1 = require("@adonisjs/ally/build/standalone");
/**
* Driver implementation. It is mostly configuration driven except the user calls
*
* ------------------------------------------------
* Change "YourDriver" to something more relevant
* ------------------------------------------------
*/
class SapcdcDriver extends standalone_1.Oauth2Driver {
constructor(ctx, config) {
super(ctx, config);
this.config = config;
/**
* The URL for the redirect request. The user will be redirected on this page
* to authorize the request.
*/
this.authorizeUrl = '';
/**
* The URL to hit to exchange the authorization code for the access token
*/
this.accessTokenUrl = '';
/**
* The URL to hit to get the user details
*/
this.userInfoUrl = '';
/**
* The param name for the authorization code. Read the documentation of your oauth
* provider and update the param name to match the query string field name in
* which the oauth provider sends the authorization_code post redirect.
*/
this.codeParamName = 'code';
/**
* The param name for the error. Read the documentation of your oauth provider and update
* the param name to match the query string field name in which the oauth provider sends
* the error post redirect
*/
this.errorParamName = 'error';
/**
* Cookie name for storing the CSRF token. Make sure it is always unique. So a better
* approach is to prefix the oauth provider name to `oauth_state` value.
*/
this.stateCookieName = 'SapcdcDriver_oauth_state';
/**
* Parameter name to be used for sending and receiving the state from.
* Read the documentation of your oauth provider and update the param
* name to match the query string used by the provider for exchanging
* the state.
*/
this.stateParamName = 'state';
/**
* Parameter name for sending the scopes to the oauth provider.
*/
this.scopeParamName = 'scope';
/**
* The separator indentifier for defining multiple scopes
*/
this.scopesSeparator = ' ';
/**
* Extremely important to call the following method to clear the
* state set by the redirect request.
*
* DO NOT REMOVE THE FOLLOWING LINE
*/
this.loadState();
}
/**
* Optionally configure the authorization redirect request. The actual request
* is made by the base implementation of "Oauth2" driver and this is a
* hook to pre-configure the request.
*/
configureRedirectRequest(request) {
request.param('scope', this.config.scopes);
request.param('response_type', this.config.responseType);
request.param('nonce', new Date().getTime());
}
/**
* Returns the HTTP request with the authorization header set
*/
getAuthenticatedRequest(url, token) {
const request = this.httpClient(url);
request.header('Authorization', `Bearer ${token}`);
request.header('Accept', 'application/json');
request.parseAs('json');
return request;
}
/**
* Fetches the user info from the Google API
*/
async getUserInfo(token, callback) {
const request = this.getAuthenticatedRequest(this.config.userInfoUrl || this.userInfoUrl, token);
if (typeof callback === 'function') {
callback(request);
}
const body = await request.get();
return {
id: body.sub,
nickName: body.name,
name: body.name,
email: body.email,
avatarUrl: body.picture,
emailVerificationState: body.email_verified ? 'verified' : 'unverified',
original: body,
};
}
/**
* Optionally configure the access token request. The actual request is made by
* the base implementation of "Oauth2" driver and this is a hook to pre-configure
* the request
*/
// protected configureAccessTokenRequest(request: ApiRequest) {}
/**
* Update the implementation to tell if the error received during redirect
* means "ACCESS DENIED".
*/
accessDenied() {
return this.ctx.request.input('error') === 'user_denied';
}
/**
* Get the user details by query the provider API. This method must return
* the access token and the user details both. Checkout the google
* implementation for same.
*
* https://github.com/adonisjs/ally/blob/develop/src/Drivers/Google/index.ts#L191-L199
*/
async user(callback) {
const accessToken = await this.accessToken();
const user = await this.getUserInfo(accessToken.token, callback);
return {
...user,
token: accessToken,
};
}
async userFromToken(accessToken, callback) {
const user = await this.getUserInfo(accessToken, callback);
return {
...user,
token: { token: accessToken, type: 'bearer' },
};
}
}
exports.SapcdcDriver = SapcdcDriver;