@koex/passport-github
Version:
koex passport-github
66 lines • 2.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GithubStrategy = void 0;
const passport_oauth2_1 = require("@koex/passport-oauth2");
class GithubStrategy extends passport_oauth2_1.Strategy {
constructor(_options, verify) {
super({
..._options,
response_type: 'code',
grant_type: 'authorization_code',
authorize_url: 'https://github.com/login/oauth/authorize',
token_url: 'https://github.com/login/oauth/access_token',
user_profile_url: 'https://api.github.com/user',
}, verify);
this._options = _options;
this.verify = verify;
}
async getAuthorizeUrl(authorize_url, data) {
return `${authorize_url}?${this.utils.qs.stringify(data)}`;
}
async getAccessToken(token_url, data) {
const method = 'POST';
const headers = {
'Content-Type': 'application/json',
Accept: 'application/json',
};
const response = await this.utils.fetch(token_url, {
method,
headers,
body: JSON.stringify(data),
});
if (!response.ok) {
const status = response.status;
const message = await response.text();
throw new Error(`[oauth.token][${status}] ${message}`);
}
return response.json();
}
async getAccessUser(user_profile_url, access_token) {
const method = 'GET';
const headers = {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${access_token.access_token}`,
};
const response = await this.utils.fetch(user_profile_url, {
method,
headers,
});
if (!response.ok) {
const status = response.status;
const message = await response.text();
throw new Error(`[oauth.user][${status}] ${message}`);
}
const data = await response.json();
return {
id: data.id,
login: data.login,
avatar_url: data.avatar_url,
url: data.url,
node_id: data.node_id,
};
}
}
exports.GithubStrategy = GithubStrategy;
//# sourceMappingURL=strategy.js.map