gitlab-acebase
Version:
AceBase realtime database server (webserver endpoint to allow remote connections)
97 lines • 4.86 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthProvider = exports.GithubAuthProvider = void 0;
const oauth_provider_1 = require("./oauth-provider");
const simple_fetch_1 = require("../shared/simple-fetch");
class GithubAuthProvider extends oauth_provider_1.OAuth2Provider {
constructor(settings) {
super(settings);
if (!settings.scopes) {
settings.scopes = [];
}
if (!settings.scopes.includes('email')) {
settings.scopes.push('email');
}
if (!settings.scopes.includes('profile')) {
settings.scopes.push('profile');
}
if (!settings.scopes.includes('openid')) {
settings.scopes.push('openid');
}
}
// async getOpenIDConfig() {
// // Get Open ID config ("The Discovery document")
// if (this._config) { return this._config; }
// this._config = await fetch(`https://${this.settings.host}/.well-known/openid-configuration`).then(res => res.json());
// return this._config;
// }
/**
* Starts auth flow by getting the url the user should be redirected to
* @param info.redirectUrl Url spotify will redirect to after authorizing, should be the url
* @param info.state Optional state that will be passed to redirectUri by spotify
*/
init(info) {
return __awaiter(this, void 0, void 0, function* () {
const authUrl = `https://github.com/login/oauth/authorize?scope=${encodeURIComponent(this.settings.scopes.join(' '))}&client_id=${this.settings.client_id}&state=${encodeURIComponent(info.state)}&redirect_uri=${encodeURIComponent(info.redirect_url)}`;
return authUrl;
});
}
getAccessToken(params) {
return __awaiter(this, void 0, void 0, function* () {
// Request access & refresh tokens with authorization code, or refresh token
// const config = await this.getOpenIDConfig();
const response = yield (0, simple_fetch_1.fetch)('https://github.com/login/oauth/access_token', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' },
body: `client_id=${this.settings.client_id}&client_secret=${this.settings.client_secret}&code=`
+ (params.type === 'refresh'
? `${params.refresh_token}&grant_type=refresh_token`
: `${params.auth_code}&redirect_uri=${encodeURIComponent(params.redirect_url)}`),
});
const result = yield response.json();
if (result.error) {
throw new Error(result.error);
}
return result;
});
}
getUserInfo(access_token) {
return __awaiter(this, void 0, void 0, function* () {
// const config = await this.getOpenIDConfig();
const response = yield (0, simple_fetch_1.fetch)('https://api.github.com/user', {
method: 'GET',
headers: { 'Authorization': `Bearer ${access_token}`, 'Accept': 'application/vnd.github+json', 'user-agent': 'node.js' },
});
const tmp = yield response.text();
const result = JSON.parse(tmp);
if (response.status !== 200) {
const error = result;
throw new Error(`${error.error}: ${error.error_description}`);
}
const user = result;
return {
id: user.id,
name: user.name,
display_name: user.name,
picture: user.avatar_url ? [{ url: user.avatar_url }] : [],
email: user.email,
email_verified: true,
other: Object.keys(user)
.filter(key => !['sub', 'name', 'picture', 'email_verified'].includes(key))
.reduce((obj, key) => { obj[key] = user[key]; return obj; }, {}),
};
});
}
}
exports.GithubAuthProvider = GithubAuthProvider;
exports.AuthProvider = GithubAuthProvider;
//# sourceMappingURL=github.js.map