mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-23 21:00:01 +08:00
Add types for simple-oauth2 library (#13655)
* Add types for simple-oauth2 library * Update config files for simple-oauth2 * apply tslint feedback * Use branding for nominal types * Remove nominal types for Tokens
This commit is contained in:
113
simple-oauth2/index.d.ts
vendored
Normal file
113
simple-oauth2/index.d.ts
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
// Type definitions for simple-oauth2 1.0
|
||||
// Project: https://github.com/lelylan/simple-oauth2
|
||||
// Definitions by: [Michael Müller] <https://github.com/mad-mike>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
import Bluebird = require("bluebird");
|
||||
|
||||
/** Creates a new simple-oauth2 client with the passed configuration */
|
||||
export function create(options: ModuleOptions): OAuthClient;
|
||||
|
||||
interface ModuleOptions {
|
||||
client: {
|
||||
/** Service registered client id. Required. */
|
||||
id: string,
|
||||
/** Service registered client secret. Required. */
|
||||
secret: string,
|
||||
/** Parameter name used to send the client secret. Default to client_secret. */
|
||||
secretParamName?: string,
|
||||
/** Parameter name used to send the client id. Default to client_id. */
|
||||
idParamName?: string
|
||||
};
|
||||
auth: {
|
||||
/** String used to set the host to request the tokens to. Required. */
|
||||
tokenHost: string,
|
||||
/** String path to request an access token. Default to /oauth/token. */
|
||||
tokenPath?: string,
|
||||
/** String path to revoken an access token. Default to /oauth/revoke. */
|
||||
revokePath?: string,
|
||||
/** String used to set the host to request an "authorization code". Default to the value set on auth.tokenHost. */
|
||||
authorizeHost?: string,
|
||||
/** String path to request an authorization code. Default to /oauth/authorize. */
|
||||
authorizePath?: string
|
||||
};
|
||||
/** optional object used to set global options to the internal http library (request-js). */
|
||||
http?: {};
|
||||
options?: {
|
||||
/** Wheather or not the client.id/client.secret params are sent in the request body. Defaults to true. */
|
||||
useBodyAuth?: boolean,
|
||||
useBasicAuthorizationHeader?: boolean
|
||||
};
|
||||
}
|
||||
|
||||
type TokenType = "access_token" | "refresh_token";
|
||||
|
||||
interface AccessToken {
|
||||
token: {};
|
||||
|
||||
/** Check if the access token is expired or not */
|
||||
expired(): boolean;
|
||||
/** Refresh the access token */
|
||||
refresh(params: {}, callback: (error: any, result: AccessToken) => void): Bluebird<AccessToken>;
|
||||
refresh(callback?: (error: any, result: AccessToken) => void): Bluebird<AccessToken>;
|
||||
/** Revoke access or refresh token */
|
||||
revoke(tokenType: TokenType, callback?: (error: any) => void): Bluebird<void>;
|
||||
}
|
||||
|
||||
interface Token {
|
||||
[x: string]: any;
|
||||
}
|
||||
type AuthorizationCode = string;
|
||||
interface AuthorizationTokenConfig {
|
||||
code: AuthorizationCode;
|
||||
redirect_uri: string;
|
||||
}
|
||||
|
||||
interface PasswordTokenConfig {
|
||||
/** A string that represents the registered username */
|
||||
username: string;
|
||||
/** A string that represents the registered password. */
|
||||
password: string;
|
||||
/** A string that represents the application privileges */
|
||||
scope: string;
|
||||
}
|
||||
|
||||
interface ClientCredentialTokenConfig {
|
||||
/** A string that represents the application privileges */
|
||||
scope?: string;
|
||||
}
|
||||
|
||||
export interface OAuthClient {
|
||||
authorizationCode: {
|
||||
/**
|
||||
* Redirect the user to the autorization page
|
||||
* @return {string} the absolute authorization url
|
||||
*/
|
||||
authorizeURL(params?: {
|
||||
/** A string that represents the registered application URI where the user is redirected after authentication */
|
||||
redirect_uri?: string,
|
||||
/** A String that represents the application privileges */
|
||||
scope?: string,
|
||||
/** A String that represents an option opaque value used by the client to main the state between the request and the callback */
|
||||
state?: string
|
||||
}): string,
|
||||
|
||||
/** Returns the Access Token object */
|
||||
getToken(params: AuthorizationTokenConfig, callback?: (error: any, result: Token) => void): Bluebird<Token>;
|
||||
};
|
||||
|
||||
ownerPassword: {
|
||||
/** Returns the Access Token Object */
|
||||
getToken(params: PasswordTokenConfig, callback?: (error: any, result: Token) => void): Bluebird<Token>;
|
||||
};
|
||||
|
||||
clientCredentials: {
|
||||
/** Returns the Access Token Object */
|
||||
getToken(params: ClientCredentialTokenConfig, callback?: (error: any, result: Token) => void): Bluebird<Token>;
|
||||
};
|
||||
|
||||
accessToken: {
|
||||
/** Creates an OAuth2.AccessToken instance */
|
||||
create(tokenToUse: Token): AccessToken;
|
||||
};
|
||||
}
|
||||
162
simple-oauth2/simple-oauth2-tests.ts
Normal file
162
simple-oauth2/simple-oauth2-tests.ts
Normal file
@@ -0,0 +1,162 @@
|
||||
// off https://github.com/lelylan/simple-oauth2/blob/master/README.md
|
||||
// slightly changed to remove external dependencies
|
||||
|
||||
// Set the configuration settings
|
||||
const credentials = {
|
||||
client: {
|
||||
id: '<client-id>',
|
||||
secret: '<client-secret>'
|
||||
},
|
||||
auth: {
|
||||
tokenHost: 'https://api.oauth.com'
|
||||
}
|
||||
};
|
||||
|
||||
// Initialize the OAuth2 Library
|
||||
//const oauth2 = require('simple-oauth2').create(credentials);
|
||||
import oauth2lib = require("simple-oauth2");
|
||||
const oauth2 = oauth2lib.create(credentials);
|
||||
|
||||
|
||||
// #Authorization Code flow
|
||||
(function () {
|
||||
|
||||
// Authorization oauth2 URI
|
||||
const authorizationUri = oauth2.authorizationCode.authorizeURL({
|
||||
redirect_uri: 'http://localhost:3000/callback',
|
||||
scope: '<scope>',
|
||||
state: '<state>'
|
||||
});
|
||||
|
||||
// Redirect example using Express (see http://expressjs.com/api.html#res.redirect)
|
||||
//res.redirect(authorizationUri);
|
||||
|
||||
// Get the access token object (the authorization code is given from the previous step).
|
||||
const tokenConfig = {
|
||||
code: '<code>',
|
||||
redirect_uri: 'http://localhost:3000/callback'
|
||||
};
|
||||
|
||||
// Callbacks
|
||||
// Save the access token
|
||||
oauth2.authorizationCode.getToken(tokenConfig, (error, result) => {
|
||||
if (error) {
|
||||
return console.log('Access Token Error', error.message);
|
||||
}
|
||||
|
||||
const token = oauth2.accessToken.create(result);
|
||||
});
|
||||
|
||||
// Promises
|
||||
// Save the access token
|
||||
oauth2.authorizationCode.getToken(tokenConfig)
|
||||
.then((result) => {
|
||||
const token = oauth2.accessToken.create(result);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log('Access Token Error', error.message);
|
||||
});
|
||||
})();
|
||||
|
||||
|
||||
// #Client Credentials Flow
|
||||
(function () {
|
||||
const tokenConfig = {};
|
||||
|
||||
// Callbacks
|
||||
// Get the access token object for the client
|
||||
oauth2.clientCredentials.getToken(tokenConfig, (error, result) => {
|
||||
if (error) {
|
||||
return console.log('Access Token Error', error.message);
|
||||
}
|
||||
|
||||
const token = oauth2.accessToken.create(result);
|
||||
});
|
||||
|
||||
|
||||
// Promises
|
||||
// Get the access token object for the client
|
||||
oauth2.clientCredentials
|
||||
.getToken(tokenConfig)
|
||||
.then((result) => {
|
||||
const token = oauth2.accessToken.create(result);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log('Access Token error', error.message);
|
||||
});
|
||||
})();
|
||||
|
||||
|
||||
// #Access Token object
|
||||
(function () {
|
||||
// Sample of a JSON access token (you got it through previous steps)
|
||||
const tokenObject = {
|
||||
'access_token': '<access-token>',
|
||||
'refresh_token': '<refresh-token>',
|
||||
'expires_in': '7200'
|
||||
};
|
||||
|
||||
// Create the access token wrapper
|
||||
var token = oauth2.accessToken.create(tokenObject);
|
||||
|
||||
// Check if the token is expired. If expired it is refreshed.
|
||||
if (token.expired()) {
|
||||
// Callbacks
|
||||
token.refresh((error, result) => {
|
||||
token = result;
|
||||
})
|
||||
|
||||
// Promises
|
||||
token.refresh()
|
||||
.then((result) => {
|
||||
token = result;
|
||||
});
|
||||
}
|
||||
|
||||
// Callbacks
|
||||
// Revoke only the access token
|
||||
token.revoke('access_token', (error) => {
|
||||
// Session ended. But the refresh_token is still valid.
|
||||
|
||||
// Revoke the refresh_token
|
||||
token.revoke('refresh_token', (error) => {
|
||||
console.log('token revoked.');
|
||||
});
|
||||
});
|
||||
|
||||
// Promises
|
||||
// Revoke only the access token
|
||||
token.revoke('access_token')
|
||||
.then(() => {
|
||||
// Revoke the refresh token
|
||||
return token.revoke('refresh_token');
|
||||
})
|
||||
.then(() => {
|
||||
console.log('Token revoked');
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log('Error revoking token.', error.message);
|
||||
});
|
||||
})();
|
||||
|
||||
|
||||
// #Errors
|
||||
// not applicable, as those errors about missing authentication codes are already found by the typescript compiler
|
||||
|
||||
// (function () {
|
||||
// // Callbacks
|
||||
// oauth2.authorizationCode.getToken({}, (error, token) => {
|
||||
// if (error) {
|
||||
// return console.log(error.message);
|
||||
// }
|
||||
// });
|
||||
|
||||
// // Promises
|
||||
// oauth2.authorizationCode
|
||||
// .getToken({})
|
||||
// .catch((error) => {
|
||||
// console.log(error.message);
|
||||
// });
|
||||
|
||||
// // => { "status": "401", "message": "Unauthorized" }
|
||||
// })();
|
||||
20
simple-oauth2/tsconfig.json
Normal file
20
simple-oauth2/tsconfig.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"simple-oauth2-tests.ts"
|
||||
]
|
||||
}
|
||||
1
simple-oauth2/tslint.json
Normal file
1
simple-oauth2/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "../tslint.json" }
|
||||
Reference in New Issue
Block a user