express-authenticators
Advanced tools
Comparing version 0.2.1 to 1.0.0-pre-1
@@ -1,15 +0,15 @@ | ||
import FacebookAuthenticator from './vendors/FacebookAuthenticator'; | ||
import FoursquareAuthenticator from './vendors/FoursquareAuthenticator'; | ||
import GithubAuthenticator from './vendors/GithubAuthenticator'; | ||
import GoogleAuthenticator from './vendors/GoogleAuthenticator'; | ||
import InstagramAuthenticator from './vendors/InstagramAuthenticator'; | ||
import LinkedInAuthenticator from './vendors/LinkedInAuthenticator'; | ||
import PinterestAuthenticator from './vendors/PinterestAuthenticator'; | ||
import TumblrAuthenticator from './vendors/TumblrAuthenticator'; | ||
import TwitterAuthenticator from './vendors/TwitterAuthenticator'; | ||
import LineAuthenticator from './vendors/LineAuthenticator'; | ||
import ZaloAuthenticator from './vendors/ZaloAuthenticator'; | ||
import AppleAuthenticator from './vendors/AppleAuthenticator'; | ||
import OAuth from './oauth/OAuth'; | ||
import OAuth2 from './oauth2/OAuth2'; | ||
export { FacebookAuthenticator, FoursquareAuthenticator, GithubAuthenticator, GoogleAuthenticator, InstagramAuthenticator, LinkedInAuthenticator, PinterestAuthenticator, TumblrAuthenticator, TwitterAuthenticator, LineAuthenticator, ZaloAuthenticator, AppleAuthenticator, OAuth2, OAuth }; | ||
import { getGoogleConsentUrl, getGoogleAccessToken, fetchGoogleProfile, refreshGoogleAccessToken, verifyGoogleIdToken } from './vendors/google'; | ||
import { getFacebookConsentUrl, getFacebookAccessToken, fetchFacebookProfile } from './vendors/facebook'; | ||
import { getAppleConsentUrl, getAppleToken, generateAppleClientSecret, verifyAppleIdToken, revokeAppleToken } from './vendors/apple'; | ||
import { getGithubConsentUrl, getGithubAccessToken, fetchGithubProfile } from './vendors/github'; | ||
import { getFoursquareConsentUrl, getFoursquareAccessToken, fetchFoursquareProfile } from './vendors/foursquare'; | ||
import { getInstagramConsentUrl, getInstagramAccessToken, fetchInstagramProfile } from './vendors/instagram'; | ||
import { getLineConsentUrl, getLineAccessToken, fetchLineProfile, refreshLineAccessToken } from './vendors/line'; | ||
import { getLinkedInConsentUrl, getLinkedInAccessToken, fetchLinkedInProfile } from './vendors/linkedIn'; | ||
import { getTwitterConsentUrl, getTwitterAccessToken, fetchTwitterProfile } from './vendors/twitter'; | ||
import { getTumblrConsentUrl, getTumblrAccessToken, fetchTumblrProfile } from './vendors/tumblr'; | ||
import { getZaloConsentUrl, getZaloAccessToken, fetchZaloProfile, refreshZaloAccessToken } from './vendors/zalo'; | ||
import { getPinterestConsentUrl, getPinterestAccessToken, fetchPinterestProfile } from './vendors/pinterest'; | ||
import { getConsentUrl, getAccessToken } from './lib/oauth'; | ||
import { getOauth1ConsentUrl, getOAuth1AccessToken, oauth1SignAndFetch } from './lib/oauth1'; | ||
export { getGoogleConsentUrl, getGoogleAccessToken, fetchGoogleProfile, refreshGoogleAccessToken, verifyGoogleIdToken, getFacebookConsentUrl, getFacebookAccessToken, fetchFacebookProfile, getAppleConsentUrl, getAppleToken, generateAppleClientSecret, verifyAppleIdToken, revokeAppleToken, getGithubConsentUrl, getGithubAccessToken, fetchGithubProfile, getFoursquareConsentUrl, getFoursquareAccessToken, fetchFoursquareProfile, getInstagramConsentUrl, getInstagramAccessToken, fetchInstagramProfile, getLineConsentUrl, getLineAccessToken, fetchLineProfile, refreshLineAccessToken, getLinkedInConsentUrl, getLinkedInAccessToken, fetchLinkedInProfile, getTwitterConsentUrl, getTwitterAccessToken, fetchTwitterProfile, getTumblrConsentUrl, getTumblrAccessToken, fetchTumblrProfile, getZaloConsentUrl, getZaloAccessToken, fetchZaloProfile, refreshZaloAccessToken, getPinterestConsentUrl, getPinterestAccessToken, fetchPinterestProfile, getConsentUrl, getAccessToken, getOauth1ConsentUrl, getOAuth1AccessToken, oauth1SignAndFetch, }; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.OAuth = exports.OAuth2 = exports.AppleAuthenticator = exports.ZaloAuthenticator = exports.LineAuthenticator = exports.TwitterAuthenticator = exports.TumblrAuthenticator = exports.PinterestAuthenticator = exports.LinkedInAuthenticator = exports.InstagramAuthenticator = exports.GoogleAuthenticator = exports.GithubAuthenticator = exports.FoursquareAuthenticator = exports.FacebookAuthenticator = void 0; | ||
const FacebookAuthenticator_1 = __importDefault(require("./vendors/FacebookAuthenticator")); | ||
exports.FacebookAuthenticator = FacebookAuthenticator_1.default; | ||
const FoursquareAuthenticator_1 = __importDefault(require("./vendors/FoursquareAuthenticator")); | ||
exports.FoursquareAuthenticator = FoursquareAuthenticator_1.default; | ||
const GithubAuthenticator_1 = __importDefault(require("./vendors/GithubAuthenticator")); | ||
exports.GithubAuthenticator = GithubAuthenticator_1.default; | ||
const GoogleAuthenticator_1 = __importDefault(require("./vendors/GoogleAuthenticator")); | ||
exports.GoogleAuthenticator = GoogleAuthenticator_1.default; | ||
const InstagramAuthenticator_1 = __importDefault(require("./vendors/InstagramAuthenticator")); | ||
exports.InstagramAuthenticator = InstagramAuthenticator_1.default; | ||
const LinkedInAuthenticator_1 = __importDefault(require("./vendors/LinkedInAuthenticator")); | ||
exports.LinkedInAuthenticator = LinkedInAuthenticator_1.default; | ||
const PinterestAuthenticator_1 = __importDefault(require("./vendors/PinterestAuthenticator")); | ||
exports.PinterestAuthenticator = PinterestAuthenticator_1.default; | ||
const TumblrAuthenticator_1 = __importDefault(require("./vendors/TumblrAuthenticator")); | ||
exports.TumblrAuthenticator = TumblrAuthenticator_1.default; | ||
const TwitterAuthenticator_1 = __importDefault(require("./vendors/TwitterAuthenticator")); | ||
exports.TwitterAuthenticator = TwitterAuthenticator_1.default; | ||
const LineAuthenticator_1 = __importDefault(require("./vendors/LineAuthenticator")); | ||
exports.LineAuthenticator = LineAuthenticator_1.default; | ||
const ZaloAuthenticator_1 = __importDefault(require("./vendors/ZaloAuthenticator")); | ||
exports.ZaloAuthenticator = ZaloAuthenticator_1.default; | ||
const AppleAuthenticator_1 = __importDefault(require("./vendors/AppleAuthenticator")); | ||
exports.AppleAuthenticator = AppleAuthenticator_1.default; | ||
const OAuth_1 = __importDefault(require("./oauth/OAuth")); | ||
exports.OAuth = OAuth_1.default; | ||
const OAuth2_1 = __importDefault(require("./oauth2/OAuth2")); | ||
exports.OAuth2 = OAuth2_1.default; | ||
exports.oauth1SignAndFetch = exports.getOAuth1AccessToken = exports.getOauth1ConsentUrl = exports.getAccessToken = exports.getConsentUrl = exports.fetchPinterestProfile = exports.getPinterestAccessToken = exports.getPinterestConsentUrl = exports.refreshZaloAccessToken = exports.fetchZaloProfile = exports.getZaloAccessToken = exports.getZaloConsentUrl = exports.fetchTumblrProfile = exports.getTumblrAccessToken = exports.getTumblrConsentUrl = exports.fetchTwitterProfile = exports.getTwitterAccessToken = exports.getTwitterConsentUrl = exports.fetchLinkedInProfile = exports.getLinkedInAccessToken = exports.getLinkedInConsentUrl = exports.refreshLineAccessToken = exports.fetchLineProfile = exports.getLineAccessToken = exports.getLineConsentUrl = exports.fetchInstagramProfile = exports.getInstagramAccessToken = exports.getInstagramConsentUrl = exports.fetchFoursquareProfile = exports.getFoursquareAccessToken = exports.getFoursquareConsentUrl = exports.fetchGithubProfile = exports.getGithubAccessToken = exports.getGithubConsentUrl = exports.revokeAppleToken = exports.verifyAppleIdToken = exports.generateAppleClientSecret = exports.getAppleToken = exports.getAppleConsentUrl = exports.fetchFacebookProfile = exports.getFacebookAccessToken = exports.getFacebookConsentUrl = exports.verifyGoogleIdToken = exports.refreshGoogleAccessToken = exports.fetchGoogleProfile = exports.getGoogleAccessToken = exports.getGoogleConsentUrl = void 0; | ||
const google_1 = require("./vendors/google"); | ||
Object.defineProperty(exports, "getGoogleConsentUrl", { enumerable: true, get: function () { return google_1.getGoogleConsentUrl; } }); | ||
Object.defineProperty(exports, "getGoogleAccessToken", { enumerable: true, get: function () { return google_1.getGoogleAccessToken; } }); | ||
Object.defineProperty(exports, "fetchGoogleProfile", { enumerable: true, get: function () { return google_1.fetchGoogleProfile; } }); | ||
Object.defineProperty(exports, "refreshGoogleAccessToken", { enumerable: true, get: function () { return google_1.refreshGoogleAccessToken; } }); | ||
Object.defineProperty(exports, "verifyGoogleIdToken", { enumerable: true, get: function () { return google_1.verifyGoogleIdToken; } }); | ||
const facebook_1 = require("./vendors/facebook"); | ||
Object.defineProperty(exports, "getFacebookConsentUrl", { enumerable: true, get: function () { return facebook_1.getFacebookConsentUrl; } }); | ||
Object.defineProperty(exports, "getFacebookAccessToken", { enumerable: true, get: function () { return facebook_1.getFacebookAccessToken; } }); | ||
Object.defineProperty(exports, "fetchFacebookProfile", { enumerable: true, get: function () { return facebook_1.fetchFacebookProfile; } }); | ||
const apple_1 = require("./vendors/apple"); | ||
Object.defineProperty(exports, "getAppleConsentUrl", { enumerable: true, get: function () { return apple_1.getAppleConsentUrl; } }); | ||
Object.defineProperty(exports, "getAppleToken", { enumerable: true, get: function () { return apple_1.getAppleToken; } }); | ||
Object.defineProperty(exports, "generateAppleClientSecret", { enumerable: true, get: function () { return apple_1.generateAppleClientSecret; } }); | ||
Object.defineProperty(exports, "verifyAppleIdToken", { enumerable: true, get: function () { return apple_1.verifyAppleIdToken; } }); | ||
Object.defineProperty(exports, "revokeAppleToken", { enumerable: true, get: function () { return apple_1.revokeAppleToken; } }); | ||
const github_1 = require("./vendors/github"); | ||
Object.defineProperty(exports, "getGithubConsentUrl", { enumerable: true, get: function () { return github_1.getGithubConsentUrl; } }); | ||
Object.defineProperty(exports, "getGithubAccessToken", { enumerable: true, get: function () { return github_1.getGithubAccessToken; } }); | ||
Object.defineProperty(exports, "fetchGithubProfile", { enumerable: true, get: function () { return github_1.fetchGithubProfile; } }); | ||
const foursquare_1 = require("./vendors/foursquare"); | ||
Object.defineProperty(exports, "getFoursquareConsentUrl", { enumerable: true, get: function () { return foursquare_1.getFoursquareConsentUrl; } }); | ||
Object.defineProperty(exports, "getFoursquareAccessToken", { enumerable: true, get: function () { return foursquare_1.getFoursquareAccessToken; } }); | ||
Object.defineProperty(exports, "fetchFoursquareProfile", { enumerable: true, get: function () { return foursquare_1.fetchFoursquareProfile; } }); | ||
const instagram_1 = require("./vendors/instagram"); | ||
Object.defineProperty(exports, "getInstagramConsentUrl", { enumerable: true, get: function () { return instagram_1.getInstagramConsentUrl; } }); | ||
Object.defineProperty(exports, "getInstagramAccessToken", { enumerable: true, get: function () { return instagram_1.getInstagramAccessToken; } }); | ||
Object.defineProperty(exports, "fetchInstagramProfile", { enumerable: true, get: function () { return instagram_1.fetchInstagramProfile; } }); | ||
const line_1 = require("./vendors/line"); | ||
Object.defineProperty(exports, "getLineConsentUrl", { enumerable: true, get: function () { return line_1.getLineConsentUrl; } }); | ||
Object.defineProperty(exports, "getLineAccessToken", { enumerable: true, get: function () { return line_1.getLineAccessToken; } }); | ||
Object.defineProperty(exports, "fetchLineProfile", { enumerable: true, get: function () { return line_1.fetchLineProfile; } }); | ||
Object.defineProperty(exports, "refreshLineAccessToken", { enumerable: true, get: function () { return line_1.refreshLineAccessToken; } }); | ||
const linkedIn_1 = require("./vendors/linkedIn"); | ||
Object.defineProperty(exports, "getLinkedInConsentUrl", { enumerable: true, get: function () { return linkedIn_1.getLinkedInConsentUrl; } }); | ||
Object.defineProperty(exports, "getLinkedInAccessToken", { enumerable: true, get: function () { return linkedIn_1.getLinkedInAccessToken; } }); | ||
Object.defineProperty(exports, "fetchLinkedInProfile", { enumerable: true, get: function () { return linkedIn_1.fetchLinkedInProfile; } }); | ||
const twitter_1 = require("./vendors/twitter"); | ||
Object.defineProperty(exports, "getTwitterConsentUrl", { enumerable: true, get: function () { return twitter_1.getTwitterConsentUrl; } }); | ||
Object.defineProperty(exports, "getTwitterAccessToken", { enumerable: true, get: function () { return twitter_1.getTwitterAccessToken; } }); | ||
Object.defineProperty(exports, "fetchTwitterProfile", { enumerable: true, get: function () { return twitter_1.fetchTwitterProfile; } }); | ||
const tumblr_1 = require("./vendors/tumblr"); | ||
Object.defineProperty(exports, "getTumblrConsentUrl", { enumerable: true, get: function () { return tumblr_1.getTumblrConsentUrl; } }); | ||
Object.defineProperty(exports, "getTumblrAccessToken", { enumerable: true, get: function () { return tumblr_1.getTumblrAccessToken; } }); | ||
Object.defineProperty(exports, "fetchTumblrProfile", { enumerable: true, get: function () { return tumblr_1.fetchTumblrProfile; } }); | ||
const zalo_1 = require("./vendors/zalo"); | ||
Object.defineProperty(exports, "getZaloConsentUrl", { enumerable: true, get: function () { return zalo_1.getZaloConsentUrl; } }); | ||
Object.defineProperty(exports, "getZaloAccessToken", { enumerable: true, get: function () { return zalo_1.getZaloAccessToken; } }); | ||
Object.defineProperty(exports, "fetchZaloProfile", { enumerable: true, get: function () { return zalo_1.fetchZaloProfile; } }); | ||
Object.defineProperty(exports, "refreshZaloAccessToken", { enumerable: true, get: function () { return zalo_1.refreshZaloAccessToken; } }); | ||
const pinterest_1 = require("./vendors/pinterest"); | ||
Object.defineProperty(exports, "getPinterestConsentUrl", { enumerable: true, get: function () { return pinterest_1.getPinterestConsentUrl; } }); | ||
Object.defineProperty(exports, "getPinterestAccessToken", { enumerable: true, get: function () { return pinterest_1.getPinterestAccessToken; } }); | ||
Object.defineProperty(exports, "fetchPinterestProfile", { enumerable: true, get: function () { return pinterest_1.fetchPinterestProfile; } }); | ||
const oauth_1 = require("./lib/oauth"); | ||
Object.defineProperty(exports, "getConsentUrl", { enumerable: true, get: function () { return oauth_1.getConsentUrl; } }); | ||
Object.defineProperty(exports, "getAccessToken", { enumerable: true, get: function () { return oauth_1.getAccessToken; } }); | ||
const oauth1_1 = require("./lib/oauth1"); | ||
Object.defineProperty(exports, "getOauth1ConsentUrl", { enumerable: true, get: function () { return oauth1_1.getOauth1ConsentUrl; } }); | ||
Object.defineProperty(exports, "getOAuth1AccessToken", { enumerable: true, get: function () { return oauth1_1.getOAuth1AccessToken; } }); | ||
Object.defineProperty(exports, "oauth1SignAndFetch", { enumerable: true, get: function () { return oauth1_1.oauth1SignAndFetch; } }); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "express-authenticators", | ||
"version": "0.2.1", | ||
"version": "1.0.0-pre-1", | ||
"description": "Third party authenticators in nodejs. Support various providers. Almost zero dependencies.", | ||
@@ -41,2 +41,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"jws": "^4.0.0", | ||
"r3986": "^0.0.3" | ||
@@ -51,2 +52,3 @@ }, | ||
"@types/jest": "^29.5.2", | ||
"@types/jws": "^3.2.5", | ||
"@types/node": "^20.3.0", | ||
@@ -53,0 +55,0 @@ "@typescript-eslint/eslint-plugin": "^5.59.9", |
231
README.md
@@ -11,5 +11,4 @@ # Express Authenticators [![Build Status](https://travis-ci.org/tranvansang/express-authenticators.svg?branch=master)](https://travis-ci.org/tranvansang/express-authenticators) | ||
- Pre-configured for popular scopes: email, profile, etc. with account fetching for basic user information. | ||
- The original OAuth/OAuth2 classes are available for customized providers. | ||
- The only dependencies are `r3986`. | ||
- Modern NodeJS. Although, it requires NodeJS >= v14.17.0 to use the `randomUUID()` function. | ||
- OAuth/OAuth2 utilities are available for customizing new providers. | ||
- The only dependencies are `r3986` and `jws` (`jws` is required for Google and Apple token check). | ||
- Strongly typed with TypeScript. | ||
@@ -26,25 +25,30 @@ - Support PKCE([Proof Key for Code Exchange](https://oauth.net/2/pkce/)). | ||
- `fetch` polyfilled. | ||
- NodeJS >= v14.17.0. | ||
- NodeJS >= v14.17.0 (to use `randomUUID()`). | ||
(before `v0.1.0`, this package was for ExpressJS only, hence its name is `express-authenticators`) | ||
## Exported APIs | ||
## Sample code in ExpressJS | ||
```typescript | ||
export { | ||
getGoogleConsentUrl, getGoogleAccessToken, fetchGoogleProfile, refreshGoogleAccessToken, verifyGoogleIdToken, | ||
getFacebookConsentUrl, getFacebookAccessToken, fetchFacebookProfile, | ||
getAppleConsentUrl, getAppleToken, generateAppleClientSecret, verifyAppleIdToken, revokeAppleToken, | ||
getGithubConsentUrl, getGithubAccessToken, fetchGithubProfile, | ||
getFoursquareConsentUrl, getFoursquareAccessToken, fetchFoursquareProfile, | ||
getInstagramConsentUrl, getInstagramAccessToken, fetchInstagramProfile, | ||
getLineConsentUrl, getLineAccessToken, fetchLineProfile, refreshLineAccessToken, | ||
getLinkedInConsentUrl, getLinkedInAccessToken, fetchLinkedInProfile, | ||
getTwitterConsentUrl, getTwitterAccessToken, fetchTwitterProfile, | ||
getTumblrConsentUrl, getTumblrAccessToken, fetchTumblrProfile, | ||
getZaloConsentUrl, getZaloAccessToken, fetchZaloProfile, refreshZaloAccessToken, | ||
getPinterestConsentUrl, getPinterestAccessToken, fetchPinterestProfile, | ||
getConsentUrl, getAccessToken, | ||
getOauth1ConsentUrl, getOAuth1AccessToken, oauth1SignAndFetch, | ||
} | ||
``` | ||
## Sample Usage | ||
```javascript | ||
const { | ||
AppleAuthenticator, | ||
FacebookAuthenticator, | ||
FoursquareAuthenticator, | ||
GithubAuthenticator, | ||
GoogleAuthenticator, | ||
LineAuthenticator, | ||
InstagramAuthenticator, | ||
LinkedInAuthenticator, | ||
PinterestAuthenticator, | ||
TumblrAuthenticator, | ||
TwitterAuthenticator, | ||
ZaloAuthenticator, | ||
OAuth2, | ||
OAuth | ||
getGoogleConsentUrl, getGoogleAccessToken, fetchGoogleProfile, refreshGoogleAccessToken, verifyGoogleIdToken, | ||
} = require('express-authenticators') | ||
@@ -57,20 +61,13 @@ const express = require('express') | ||
const facebookAuth = new FacebookAuthenticator({ | ||
clientID: 'facebook app id', | ||
clientSecret: 'facebook app secret', | ||
redirectUri: `https://example.com/auth/facebook/callback`, | ||
}) | ||
app.get( | ||
'/auth/facebook', | ||
'/auth/google', | ||
async (req, res, next) => { | ||
req.session.someInfo = 'my info' // store the user credential | ||
try { | ||
const redirectUrl = await facebookAuth.authenticate({ | ||
store(token) { | ||
req.session.oauthFacebook = token | ||
} | ||
}) | ||
res.status = 302 | ||
res.redirect(redirectUrl) | ||
const {url, state} = await getGoogleConsentUrl({ | ||
clientID: 'your client id', | ||
redirectUri: 'https://your-host.com/auth/google/callback', | ||
}) | ||
req.session.oauthGoogle = JSON.stringify(state) | ||
res.redirect(302, url) | ||
} catch (e) { | ||
@@ -82,10 +79,15 @@ next(e) | ||
app.get( // for AppleAuthenticator, must use POST method instead | ||
`/auth/facebook/callback`, | ||
'/auth/google/callback', | ||
async (req, res, next) => { | ||
try { | ||
const payload = await facebookAuth.callback( | ||
req.session.oauthFacebook, | ||
new URL(`https://example.com${req.url}`).search // for AppleAuthenticator, use req.body instead | ||
const {access_token} = await getGoogleAccessToken( | ||
{ | ||
clientID: 'your client id', | ||
clientSecret: 'your client secret', | ||
redirectUri: 'https://your-host.com/auth/google/callback', | ||
}, | ||
JSON.parse(req.session.oauthGoogle), | ||
Object.fromEntries(new URLSearchParams(new URL(`https://example.com${req.url}`).search)) // for AppleAuthenticator, use req.body instead | ||
) | ||
const profile = await facebookAuth.fetchProfile(payload) // not supported by AppleAuthenticator | ||
const profile = await fetchGoogleProfile(access_token) | ||
console.log('got profile', profile) | ||
@@ -100,61 +102,8 @@ res.send(JSON.stringify(profile)) | ||
# API references | ||
## Profile interface | ||
## Exported classes | ||
All fetch profile APIs return the same interface: | ||
- 2 generic classes: `OAuth2` and `OAuth`. | ||
- Pre-configured providers that inherit `OAuth`: `TwitterAuthenticator`, `TumblrAuthenticator`. | ||
- Pre-configured providers that inherit `OAuth2`: | ||
- `AppleAuthenticator` | ||
- `FacebookAuthenticator` | ||
- `FoursquareAuthenticator` | ||
- `GithubAuthenticator` | ||
- `GoogleAuthenticator` | ||
- `InstagramAuthenticator` | ||
- `LinkedInAuthenticator` | ||
- `PinterestAuthenticator` | ||
- `LineAuthenticator` | ||
- `ZaloAuthenticator` | ||
## Constructors | ||
- All pre-configured providers' constructors take only one parameter: `options` with the following properties. | ||
```typescript | ||
{ | ||
clientID: string | ||
clientSecret: string // not required for AppleAuthenticator | ||
redirectUri: string | ||
} | ||
``` | ||
## Most generic methods | ||
All exported classes inherit the `IOAuthCommon` interface which has the following methods: | ||
- `authenticate(session: {store(token: string): void | Promise<void>}): string | Promise<string>`. | ||
- Input: this method takes only one argument, `session` whose `store` method is called with a token in `string` type | ||
to store in the request session. This data will be required in the succeeding `callback()` method. | ||
- Output: redirect url. The controller/router should redirect the user to this url. This function always returns | ||
a `string` type or throws an error if it fails. | ||
- `callback({pop}: {pop(): string | undefined}, rawQuery: string)`: | ||
- Input: `pop` is a function that returns the token from the request session. This token is required to validate the | ||
authentication. | ||
- Input: `rawQuery` is the query string from the callback url, the query may or may not contain the leading `?` character (internally, we use `URLSearchParams` which handles this automatically). | ||
- Output: the token payload returned from the provider. For `OAuth` providers, this | ||
is `{token: string, secret: string}`. For `OAuth2` providers, the payload is the JSON-parsed response from the | ||
provider which usually contains the token for further request. | ||
## Pre-configured providers' methods | ||
Pre-configured providers have the following methods: | ||
- `fetchProfile(tokenPayload): Promise<IOAuthProfile>` (not available with AppleAuthenticator): takes the token payload returned from the `callback()` method | ||
and returns the profile data. Although each provider returns different data, they are all pre-configured in this | ||
library to return the `IOAuthProfile` described below. | ||
```typescript | ||
export interface IOAuthProfile { | ||
interface OAuthProfile { | ||
id?: string | ||
@@ -170,89 +119,3 @@ email?: string | ||
Where `raw` is the raw JSON-parsed data returned from the provider. Other fields are calculated **carefully** based on | ||
the data returned from the provider. | ||
## Customized provider | ||
While I recommend you using the pre-configured providers, you can also create your own customized provider by extending | ||
the `OAuth`/`OAuth2` classes or initialize a new instance of the `OAuth`/`OAuth2` classes directly. | ||
Here are two sample implementations of `FacebookAuthenticator` (extending `OAuth2`), and `TwitterAuthenticator` ( | ||
extending `OAuth`) | ||
```typescript | ||
class FacebookAuthenticator | ||
extends OAuth2<IFacebookTokenPayload> | ||
implements IOAuthProfileFetcher<IFacebookTokenPayload> { | ||
fetchProfile = fetchFacebookProfile | ||
constructor(options: { | ||
clientID: string | ||
clientSecret: string | ||
redirectUri: string | ||
scope?: string | ||
}) { | ||
super({ | ||
consentURL: 'https://www.facebook.com/v9.0/dialog/oauth', | ||
tokenURL: 'https://graph.facebook.com/v9.0/oauth/access_token', | ||
scope: ['email'].join(','), | ||
...options, | ||
}, { | ||
ignoreGrantType: true, | ||
tokenRequestMethod: TokenRequestMethod.GET, | ||
includeStateInAccessToken: false, | ||
enablePKCE: false, | ||
}) | ||
} | ||
} | ||
export default class TwitterAuthenticator extends OAuth implements IOAuthProfileFetcher<IOAuthTokenPayload> { | ||
constructor(config: { | ||
clientID: string | ||
clientSecret: string | ||
redirectUri: string | ||
}) { | ||
super({ | ||
consumerKey: config.clientID, | ||
consumerSecret: config.clientSecret, | ||
callbackUrl: config.redirectUri, | ||
requestTokenUrl: 'https://api.twitter.com/oauth/request_token', | ||
accessTokenUrl: 'https://api.twitter.com/oauth/access_token', | ||
authorizeUrl: 'https://api.twitter.com/oauth/authorize', | ||
signingMethod: OAuthSigningMethod.Hmac, | ||
}) | ||
} | ||
async fetchProfile(tokenPayload: IOAuthTokenPayload) { | ||
const response = await this.signAndFetch( | ||
'https://api.twitter.com/1.1/account/verify_credentials.json', | ||
{ | ||
qs: {include_email: true}, | ||
}, | ||
tokenPayload | ||
) | ||
if (!response.ok) throw new OAuthProfileError(await response.text()) | ||
const profile = await response.json() | ||
if (!profile.id_str) throw new OAuthProfileError('Invalid Twitter profile ID') | ||
return { | ||
id: profile.id_str, | ||
raw: profile, | ||
avatar: profile.profile_image_url_https | ||
|| profile.profile_image_url | ||
|| profile.profile_background_image_url_https | ||
|| profile.profile_background_image_url, | ||
first: profile.name || profile.screen_name, | ||
email: profile.email, | ||
emailVerified: !!profile.email, | ||
/** | ||
* from twitter docs | ||
* https://developer.twitter.com/en/docs/accounts-and-users | ||
* /manage-account-settings/api-reference/get-account-verify_credentials | ||
* When set to true email will be returned in the user objects as a string. | ||
* If the user does not have an email address on their account, | ||
* or if the email address is not verified, null will be returned. | ||
*/ | ||
} | ||
} | ||
} | ||
``` | ||
Where `raw` is the raw JSON-parsed data returned from the provider. | ||
Other fields are calculated **carefully** based on the data returned from each provider. |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
147552
2057
4
2
19
53
116
+ Addedjws@^4.0.0
+ Addedbuffer-equal-constant-time@1.0.1(transitive)
+ Addedecdsa-sig-formatter@1.0.11(transitive)
+ Addedjwa@2.0.0(transitive)
+ Addedjws@4.0.0(transitive)
+ Addedsafe-buffer@5.2.1(transitive)