Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as uuidv4 from 'uuid/v4';
- import moment = require("moment");
- export class AuthService {
- private userStates: Array<any> = new Array<any>();
- constructor(private config) {
- }
- public createAuthUrl(userId: string) {
- this.userStates = new Array<any>();
- let state = {
- securityToken: uuidv4(),
- userId: userId
- };
- this.signOut(userId);
- this.userStates.push(state);
- return `${this.config.baseUrl}/html/auth-start.html?authorizationUrl=` + encodeURIComponent(`https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=${this.config.bot.appId}&response_type=code&response_mode=query&scope=offline_access%20User.ReadBasic.All%20People.Read&state=${JSON.stringify(state)}&redirect_uri=${this.config.baseUrl}/callback`);
- }
- public getUserState(userId: string) {
- return this.userStates.find(user => user.userId == userId);
- }
- public verifyToken(token: string, userId: string): boolean {
- const storedState = this.userStates.find(state => state.securityToken === token);
- const tokenVerified = storedState.userId === userId;
- if (tokenVerified) {
- storedState.verified = true;
- }
- return tokenVerified;
- }
- public async associateAuthKey(code: any, savedState: any) {
- const res = await fetch("https://login.microsoftonline.com/common/oauth2/v2.0/token",
- {
- method: 'POST',
- body: `client_secret=${this.config.bot.appPassword}&grant_type=authorization_code&client_id=${this.config.bot.appId}&scope=offline_access%20User.ReadBasic.All%20People.Read&code=${code}&redirect_uri=${encodeURIComponent(this.config.baseUrl + "/callback")}`,
- headers:
- {
- "Content-Type": "application/x-www-form-urlencoded"
- }
- });
- const json = await res.json();
- savedState.accessToken = json.access_token;
- savedState.refreshToken = json.refresh_token;
- savedState.expiresAt = moment().add(json.expires_in-60, "s");
- }
- public async getAuthKey(userId: string): Promise<string> {
- const state = this.getUserState(userId);
- if (state && state.verified) {
- if (moment().isBefore(state.expiresAt)) {
- return state.accessToken;
- } else {
- const res = await fetch("https://login.microsoftonline.com/common/oauth2/v2.0/token",
- {
- method: 'POST',
- body: `client_secret=${this.config.bot.appPassword}&grant_type=refresh_token&client_id=${this.config.bot.appId}&refresh_token=${state.refreshToken}`,
- headers:
- {
- "Content-Type": "application/x-www-form-urlencoded"
- }
- });
- const json = await res.json();
- state.accessToken = json.access_token;
- state.refreshToken = json.refresh_token;
- state.expiresAt = moment().add(json.expires_in-60, "s");
- return state.accessToken;
- }
- }
- }
- public signedIn(userId: string): boolean {
- return this.userStates.some(state => state.userId == userId && state.verified);
- }
- public signOut(userId: string) {
- this.userStates = this.userStates.filter(state => state.userId !== userId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement