Typings for oauth2orize (#12712)

* Typings for oauth2orize

* Adopt @andy-ms ' review

* Change IssuedFunction more simpler
This commit is contained in:
Kei Son 2016-11-22 11:23:56 +09:00 committed by Andy
parent b6b22d52ee
commit 2bf6317d73
3 changed files with 183 additions and 0 deletions

79
oauth2orize/index.d.ts vendored Normal file
View File

@ -0,0 +1,79 @@
// Type definitions for oauth2orize v1.5.1
// Project: https://github.com/jaredhanson/oauth2orize/
// Definitions by: Wonshik Kim <https://github.com/wokim/>, Kei Son <https://github.com/heycalmdown>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference types="node" />
import * as http from "http";
interface ServerOptions {
store: any;
loadTransaction: boolean;
}
export function createServer(options?: ServerOptions): OAuth2Server;
export interface AuthorizeOptions {
idLength?: number;
sessionKey?: string;
}
export interface ErrorHandlerOptions {
mode?: string;
}
type MiddlewareFunction = (req: http.IncomingMessage, res: http.ServerResponse, next: Function) => void;
type ValidatedFunction = (err: Error | null, client?: any, redirectURI?: string) => void;
type IssuedFunction = (err: Error | null, accessToken?: string | boolean, refreshToken?: string, params?: any) => void;
export class OAuth2Server {
exchange(fn: MiddlewareFunction): OAuth2Server;
exchange(type: string, fn: MiddlewareFunction): OAuth2Server;
// Parses requests to obtain authorization
authorize (options: AuthorizeOptions, validate: (clientId: string, redirectURI: string, validated: ValidatedFunction) => void): MiddlewareFunction;
authorization(options: AuthorizeOptions, validate: (clientId: string, redirectURI: string, validated: ValidatedFunction) => void): MiddlewareFunction;
authorize (validate: (clientId: string, redirectURI: string, validated: ValidatedFunction) => void): MiddlewareFunction;
authorization(validate: (clientId: string, redirectURI: string, validated: ValidatedFunction) => void): MiddlewareFunction;
token(options?: any): MiddlewareFunction;
errorHandler(options?: any): (err: Error, req: http.IncomingMessage, res: http.ServerResponse, next: any) => void;
serializeClient(fn: (client: any, done: (err: Error | null, id: string) => void) => void): void;
serializeClient(client: any, done: (err: Error | null, id: string) => void): void;
deserializeClient(fn: (id: string, done: (err: Error | null, client?: any | boolean) => void) => void): void;
deserializeClient(obj: any, done: (err: Error | null, client?: any | boolean) => void): void;
}
export namespace exchange {
interface Options {
// The 'user' property of `req` holds the authenticated user. In the case
// of the token endpoint, the property will contain the OAuth 2.0 client.
userProperty?: string;
// For maximum flexibility, multiple scope spearators can optionally be
// allowed. This allows the server to accept clients that separate scope
// with either space or comma (' ', ','). This violates the specification,
// but achieves compatibility with existing client libraries that are already
// deployed.
scopeSeparator?: string;
}
function authorizationCode(options: Options, issue: (client: any, code: string, redirectURI: string, issued: IssuedFunction) => void): MiddlewareFunction;
function authorizationCode(issue: (client: any, code: string, redirectURI: string, issued: IssuedFunction) => void): MiddlewareFunction;
function code(options: Options, issue: (client: any, code: string, redirectURI: string, issued: IssuedFunction) => void): MiddlewareFunction;
function code(issue: (client: any, code: string, redirectURI: string, issued: IssuedFunction) => void): MiddlewareFunction;
function clientCredentials(options: Options, issue: (client: any, scope: string[], issued: IssuedFunction) => void): MiddlewareFunction;
function clientCredentials(options: Options, issue: (client: any, issued: IssuedFunction) => void): MiddlewareFunction;
function clientCredentials(issue: (client: any, scope: string[], issued: IssuedFunction) => void): MiddlewareFunction;
function clientCredentials(issue: (client: any, issued: IssuedFunction) => void): MiddlewareFunction;
function password(options: Options, issue: (client: any, username: string, password: string, scope: string[], issued: IssuedFunction) => void): MiddlewareFunction;
function password(options: Options, issue: (client: any, username: string, password: string, issued: IssuedFunction) => void): MiddlewareFunction;
function password(issue: (client: any, username: string, password: string, scope: string[], issued: IssuedFunction) => void): MiddlewareFunction;
function password(issue: (client: any, username: string, password: string, issued: IssuedFunction) => void): MiddlewareFunction;
function refreshToken(options: Options, issue: (client: any, refreshToken: string, scope: string[], issued: IssuedFunction) => void): MiddlewareFunction;
function refreshToken(options: Options, issue: (client: any, refreshToken: string, issued: IssuedFunction) => void): MiddlewareFunction;
function refreshToken(issue: (client: any, refreshToken: string, scope: string[], issued: IssuedFunction) => void): MiddlewareFunction;
function refreshToken(issue: (client: any, refreshToken: string, issued: IssuedFunction) => void): MiddlewareFunction;
}

View File

@ -0,0 +1,84 @@
import * as oauth2orize from 'oauth2orize';
import * as http from 'http';
// from https://github.com/jaredhanson/oauth2orize/
// Create an OAuth Server
const server = oauth2orize.createServer();
// Register Grants
// server.grant(oauth2orize.grant.code(function(client, redirectURI, user, ares, done) {
// var code = utils.uid(16);
// var ac = new AuthorizationCode(code, client.id, redirectURI, user.id, ares.scope);
// ac.save(function(err) {
// if (err) { return done(err); }
// return done(null, code);
// });
// }));
// Register Exchanges
class AuthorizationCode {
static findOne(code: string, callback: (err: Error, code: {
clientId: string, userId: string, redirectURI: string, scope: string
}) => void): void {}
}
server.exchange(oauth2orize.exchange.code(function(client, code, redirectURI, done) {
AuthorizationCode.findOne(code, function(err, code) {
if (err) { return done(err); }
if (client.id !== code.clientId) { return done(null, false); }
if (redirectURI !== code.redirectURI) { return done(null, false); }
// var token = utils.uid(256);
// var at = new AccessToken(token, code.userId, code.clientId, code.scope);
// at.save(function(err) {
// if (err) { return done(err); }
// return done(null, token);
// });
});
}));
// Implement Authorization Endpoint
class Clients {
static findOne(id: string, callback: (err: Error, client?: Clients) => void): void {
callback(new Error(), {} as Clients);
}
redirectURI: string;
}
// app.get('/dialog/authorize',
// login.ensureLoggedIn(),
server.authorize(function(clientID, redirectURI, done) {
Clients.findOne(clientID, function(err, client) {
if (err) { return done(err); }
if (!client) { return done(null, false); }
if (client.redirectURI != redirectURI) { return done(null, false); }
return done(null, client, client.redirectURI);
});
}),
function(req: http.IncomingMessage, res: http.ServerResponse) {
// res.render('dialog', { transactionID: req.oauth2.transactionID,
// user: req.user, client: req.oauth2.client });
}
// );
// Session Serialization
server.serializeClient(function(client, done) {
return done(null, client.id);
});
server.deserializeClient(function(id, done) {
Clients.findOne(id, function(err, client) {
if (err) { return done(err); }
return done(null, client);
});
});
// Implement Token Endpoint
// app.post('/token',
// passport.authenticate(['basic', 'oauth2-client-password'], { session: false }),
server.token(),
server.errorHandler()
// );

20
oauth2orize/tsconfig.json Normal file
View File

@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es6",
"noImplicitAny": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"oauth2orize-tests.ts"
]
}