adonis-ally-vk
Version:
A driver of AdonisJS Ally for vk.com
168 lines (167 loc) • 6.02 kB
JavaScript
"use strict";
/*
* adonis-ally-vk
*
* (c) Lookin Anton <lookin@lookinlab.ru>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.VkDriver = void 0;
const standalone_1 = require("@adonisjs/ally/build/standalone");
/**
* Vkontakte driver to login user via vk.com
*/
class VkDriver extends standalone_1.Oauth2Driver {
constructor(ctx, config) {
super(ctx, config);
this.config = config;
/**
* The version of api vk.com
*/
this.apiVersion = '5.131';
/**
* The URL for the redirect request. The user will be redirected on this page
* to authorize the request.
*/
this.authorizeUrl = 'https://oauth.vk.com/authorize';
/**
* The URL to hit to exchange the authorization code for the access token
*/
this.accessTokenUrl = 'https://oauth.vk.com/access_token';
/**
* The URL to hit to get the user details
*/
this.userInfoUrl = 'https://api.vk.com/method/users.get';
/**
* 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 = 'vk_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.
*/
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) {
/**
* Define user defined scopes of the default one`s
*/
request.scopes(this.config.scopes || ['email']);
request.param('display', this.config.display || 'page');
request.param('response_type', 'code');
}
/**
* 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.
*/
async user(callback) {
const accessToken = await this.accessToken();
const user = await this.getUserInfo(accessToken.token, callback);
return {
...user,
email: accessToken.email,
token: accessToken,
};
}
/**
* Finds the user by the access token
*/
async userFromToken(accessToken, callback) {
const user = await this.getUserInfo(accessToken, callback);
return {
...user,
email: null,
token: { token: accessToken, type: 'bearer' },
};
}
/**
* Returns the request with access_token and version api
*/
getAuthenticatedRequest(token) {
const request = this.httpClient(this.config.userInfoUrl || this.userInfoUrl);
request.header('Accept', 'application/json');
request.param('access_token', token);
request.param('v', this.apiVersion);
request.parseAs('json');
return request;
}
/**
* Returns a user info
*/
async getUserInfo(token, callback) {
const request = this.getAuthenticatedRequest(token);
/**
* Define user defined fields of the default one`s
*/
if (this.config.fields) {
request.param('fields', this.config.fields.join(','));
}
/**
* Allow end user to configure the request. This should be called after your custom
* configuration, so that the user can override them (if required)
*/
if (typeof callback === 'function') {
callback(request);
}
const user = await request.get().then((data) => data?.response[0]);
const name = `${user?.last_name} ${user?.first_name}`;
const screenName = user?.screen_name || name;
return {
id: `id${user.id}`,
name: name,
nickName: screenName,
emailVerificationState: 'unsupported',
avatarUrl: null,
original: user,
};
}
}
exports.VkDriver = VkDriver;