easy-social-auth
Version:
A flexible, standalone package for social authentication using Google, Facebook & Twitter
85 lines (84 loc) • 4.01 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthStrategy = void 0;
const axios_1 = __importDefault(require("axios"));
const grant_type_enum_1 = require("../enums/grant-type.enum");
class AuthStrategy {
constructor(clientId, clientSecret, userInfoEndpoint, tokenEndpoint, authUrl, grantType = grant_type_enum_1.GrantType.AUTHORIZATION_CODE) {
this.clientId = clientId;
this.clientSecret = clientSecret;
this.userInfoEndpoint = userInfoEndpoint;
this.tokenEndpoint = tokenEndpoint;
this.authUrl = authUrl;
this.grantType = grantType;
}
generateAuthUrl(redirectUri, scope, responseType, additionalParams = {}) {
const url = new URL(this.authUrl);
url.searchParams.set("client_id", this.clientId);
url.searchParams.set("redirect_uri", redirectUri);
url.searchParams.set("response_type", responseType !== null && responseType !== void 0 ? responseType : "code");
if (scope)
url.searchParams.set("scope", scope);
Object.keys(additionalParams).forEach((param) => {
url.searchParams.append(param, additionalParams[param]);
});
return url.toString();
}
exchangeToken(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
try {
const { data } = yield axios_1.default.post(this.tokenEndpoint, params);
return { status: true, data: data === null || data === void 0 ? void 0 : data.access_token };
}
catch (error) {
return {
status: false,
error: ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error_description) || error.message,
};
}
});
}
exchangeCodeForToken(code_1, redirectUri_1) {
return __awaiter(this, arguments, void 0, function* (code, redirectUri, additionalParams = {}) {
const params = Object.assign({ code, client_id: this.clientId, client_secret: this.clientSecret, redirect_uri: redirectUri, grant_type: this.grantType }, additionalParams);
return this.exchangeToken(params);
});
}
refreshAccessToken(refreshToken) {
return __awaiter(this, void 0, void 0, function* () {
const params = {
refresh_token: refreshToken,
client_id: this.clientId,
client_secret: this.clientSecret,
grant_type: grant_type_enum_1.GrantType.REFRESH_TOKEN,
};
return this.exchangeToken(params);
});
}
exchangePasswordForToken(username, password) {
return __awaiter(this, void 0, void 0, function* () {
const params = {
username,
password,
client_id: this.clientId,
client_secret: this.clientSecret,
grant_type: grant_type_enum_1.GrantType.PASSWORD,
};
return this.exchangeToken(params);
});
}
}
exports.AuthStrategy = AuthStrategy;