express-authenticators
Advanced tools
Comparing version 0.0.1 to 0.0.2
import { OAuthSigningMethod } from './oauthUtils'; | ||
import { Request, Response } from 'express'; | ||
import { Request } from 'express'; | ||
import { IOAuthCommon } from '../OAuthCommon'; | ||
@@ -26,2 +26,3 @@ declare type IHttpMethod = 'POST' | 'GET'; | ||
private config; | ||
authenticate: import("express").RequestHandler<import("express-serve-static-core").ParamsDictionary>; | ||
constructor(config: { | ||
@@ -37,3 +38,2 @@ consumerKey: string; | ||
signAndFetch(url: string, options: IOAuthRequestOptions, tokenSet?: IOAuthTokenSet): Promise<import("node-fetch").Response>; | ||
authenticate(req: Request, res: Response): Promise<void>; | ||
callback(req: Request): Promise<{ | ||
@@ -40,0 +40,0 @@ token: any; |
@@ -58,2 +58,3 @@ "use strict"; | ||
var r3986_1 = __importDefault(require("r3986")); | ||
var middleware_async_1 = __importDefault(require("middleware-async")); | ||
var version = '1.0'; | ||
@@ -63,13 +64,5 @@ var sessionKey = 'oauth'; | ||
function OAuth(config) { | ||
var _this = this; | ||
this.config = config; | ||
} | ||
OAuth.prototype.signAndFetch = function (url, options, tokenSet) { | ||
return node_fetch_1.default("" + url + (options.qs ? "?" + qs_1.default.stringify(options.qs) : ''), { | ||
headers: __assign(__assign({}, options.headers), { Authorization: this.authorizationHeader(url, options, tokenSet) }), | ||
method: options.method, | ||
body: options.body && qs_1.default.stringify(options.body), | ||
}); | ||
}; | ||
OAuth.prototype.authenticate = function (req, res) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
this.authenticate = middleware_async_1.default(function (req, res) { return __awaiter(_this, void 0, void 0, function () { | ||
var response, _a, _b, oauth_token, oauth_token_secret, oauth_callback_confirmed, _c, _d; | ||
@@ -104,2 +97,9 @@ return __generator(this, function (_e) { | ||
}); | ||
}); }); | ||
} | ||
OAuth.prototype.signAndFetch = function (url, options, tokenSet) { | ||
return node_fetch_1.default("" + url + (options.qs ? "?" + qs_1.default.stringify(options.qs) : ''), { | ||
headers: __assign(__assign({}, options.headers), { Authorization: this.authorizationHeader(url, options, tokenSet) }), | ||
method: options.method, | ||
body: options.body && qs_1.default.stringify(options.body), | ||
}); | ||
@@ -106,0 +106,0 @@ }; |
@@ -1,4 +0,4 @@ | ||
import { Request, Response } from 'express'; | ||
import { Request, RequestHandler } from 'express'; | ||
export interface IOAuthCommon<T> { | ||
authenticate(req: Request, res: Response): Promise<void> | void; | ||
authenticate: RequestHandler; | ||
callback(req: Request): Promise<T> | T; | ||
@@ -5,0 +5,0 @@ } |
{ | ||
"name": "express-authenticators", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Third party authenticators in nodejs. Support various providers. Zero heavy dependencies.", | ||
@@ -10,2 +10,4 @@ "main": "index.js", | ||
"keywords": [ | ||
"passport", | ||
"passportjs", | ||
"express", | ||
@@ -28,2 +30,4 @@ "authenticator", | ||
"build": "yarn tsc", | ||
"test": "yarn jest", | ||
"coverage": "yarn jest --coverage", | ||
"lint": "yarn tslint -p tsconfig.json && (yarn eslint index.ts src/**/*.ts || true)" | ||
@@ -38,2 +42,3 @@ }, | ||
"crypto": "^1.0.1", | ||
"middleware-async": "^1.1.3", | ||
"node-fetch": "^2.6.0", | ||
@@ -40,0 +45,0 @@ "qs": "^6.9.3", |
@@ -45,4 +45,3 @@ # Express Authenticators [![Build Status](https://travis-ci.org/tranvansang/express-authenticators.svg?branch=master)](https://travis-ci.org/tranvansang/express-authenticators) | ||
`authenticate`: async middleware. Your must wrap this middleware by `asyncMiddleware`. | ||
Otherwise, if error occurred, it will not be handled by next middleware, leading to a timeout request. | ||
`authenticate`: redirect middleware | ||
If there is no error, this middleware will redirect the user to the provider website. | ||
@@ -166,9 +165,9 @@ Eventually, redirect back to our pre-configured `redirectUri` with appropriate user privilege. | ||
'/auth/facebook', | ||
asyncMiddleware(async (req, res) => { | ||
(req, res, next) => { | ||
req.session!.someInfo = 'my info' // do something e.g. | ||
await facebookAuth.authenticate(req, res) | ||
}) | ||
facebookAuth.authenticate(req, res, next) | ||
} | ||
/* | ||
or | ||
app.get('/auth/facebook', asyncMiddleware(facebookAuth.authenticate)) | ||
app.get('/auth/facebook', facebookAuth.authenticate) | ||
*/ | ||
@@ -175,0 +174,0 @@ ) |
@@ -8,2 +8,3 @@ import fetch from 'node-fetch' | ||
import {IOAuthCommon} from '../OAuthCommon' | ||
import asyncMiddleware from 'middleware-async' | ||
@@ -27,2 +28,25 @@ type IHttpMethod = 'POST' | 'GET' | ||
export default class OAuth implements IOAuthCommon<IOAuthTokenSet> { | ||
authenticate = asyncMiddleware(async (req: Request, res: Response) => { | ||
const response = await this.signAndFetch( | ||
this.config.requestTokenUrl, | ||
{ | ||
method: 'POST', | ||
oauthHeaders: { | ||
oauth_callback: this.config.callbackUrl, | ||
} | ||
} | ||
) | ||
if (!response.ok) throw new OAuthError(await response.text()) | ||
const { | ||
oauth_token, | ||
oauth_token_secret, | ||
oauth_callback_confirmed | ||
} = qs.parse(await response.text()) | ||
if (oauth_callback_confirmed !== 'true') throw new Error('Failed to request access token') | ||
req.session![sessionKey] = { | ||
secret: oauth_token_secret | ||
} | ||
res.status(302).redirect(`${this.config.authorizeUrl}?${qs.stringify({oauth_token})}`) | ||
}) | ||
constructor( | ||
@@ -55,24 +79,2 @@ private config: { | ||
async authenticate(req: Request, res: Response) { | ||
const response = await this.signAndFetch( | ||
this.config.requestTokenUrl, | ||
{ | ||
method: 'POST', | ||
oauthHeaders: { | ||
oauth_callback: this.config.callbackUrl, | ||
} | ||
} | ||
) | ||
if (!response.ok) throw new OAuthError(await response.text()) | ||
const { | ||
oauth_token, | ||
oauth_token_secret, | ||
oauth_callback_confirmed | ||
} = qs.parse(await response.text()) | ||
if (oauth_callback_confirmed !== 'true') throw new Error('Failed to request access token') | ||
req.session![sessionKey] = { | ||
secret: oauth_token_secret | ||
} | ||
res.status(302).redirect(`${this.config.authorizeUrl}?${qs.stringify({oauth_token})}`) | ||
} | ||
async callback(req: Request){ | ||
@@ -79,0 +81,0 @@ const {oauth_token, oauth_verifier} = req.query |
@@ -1,5 +0,5 @@ | ||
import {Request, Response} from 'express' | ||
import {Request, RequestHandler} from 'express' | ||
export interface IOAuthCommon<T> { | ||
authenticate(req: Request, res: Response): Promise<void> | void | ||
authenticate: RequestHandler | ||
callback(req: Request): Promise<T> | T | ||
@@ -6,0 +6,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
239394
5103
11
184
+ Addedmiddleware-async@^1.1.3
+ Addedmiddleware-async@1.4.0(transitive)