Skip to content

Commit

Permalink
feat(types): Added further types
Browse files Browse the repository at this point in the history
close #41
  • Loading branch information
Retro64 committed Apr 19, 2017
1 parent 0e79ec7 commit 6a44d09
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 34 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from 'lib-oauth-tooling';
```

#### TokenCache(tokenConfig: any, oauthConfig: OAuthConfig)
#### TokenCache(tokenConfig: { [key : string]: string[] }, oauthConfig: OAuthConfig)

Class to request and cache tokens on client-side.

Expand Down Expand Up @@ -84,7 +84,7 @@ app.get('/secured/route', requireScopesMiddleware(['scopeA', 'scopeB']), (reques
})
```
#### getTokenInfo(tokenInfoEndpoint: string, accessToken: string): Promise<any>
#### getTokenInfo(tokenInfoEndpoint: string, accessToken: string): Promise<TokenInfo>
Makes a request to the `tokenInfoEndpoint` to validate the given `accessToken`.
Expand Down
2 changes: 1 addition & 1 deletion integration-test/mock-tooling/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe('mock tooling', () => {

// when
let promise = getAccessToken(options)
.then((token : TokenInfo) => {
.then((token : Token) => {

return getTokenInfo(tokeninfoEndpoint, token.access_token);
});
Expand Down
4 changes: 2 additions & 2 deletions integration-test/oauth-tooling/getAccessToken.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('getAccessToken', () => {
let authenticationServer: Http.Server;
let authServerApp: Express.Application;

let getAccessTokenOptions: any;
let getAccessTokenOptions : OAuthConfig;

// Setup AuthServer
beforeEach(() => {
Expand Down Expand Up @@ -127,7 +127,7 @@ describe('getAccessToken', () => {

describe('authorization code grant', () => {

let getAccessTokenOptionsAuthorization: any;
let getAccessTokenOptionsAuthorization: OAuthConfig​​ ;

before(() => {
getAccessTokenOptionsAuthorization = {
Expand Down
14 changes: 7 additions & 7 deletions src/TokenCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const EXPIRE_THRESHOLD = 60 * 1000;
*/
class TokenCache {

private _tokens : any = {};
private _tokens: { [key: string]: TokenInfo } = {};

/**
* `oauthConfig`:
Expand All @@ -36,7 +36,7 @@ class TokenCache {
* @param tokenConfig
* @param oauthConfig
*/
constructor(private tokenConfig : any, private oauthConfig : OAuthConfig) {
constructor(private tokenConfig : { [key : string]: string[] }, private oauthConfig : OAuthConfig) {

validateOAuthConfig(oauthConfig);

Expand Down Expand Up @@ -65,7 +65,7 @@ constructor(private tokenConfig : any, private oauthConfig : OAuthConfig) {
* @param tokenName
* @returns {Promise<T>}
*/
get(tokenName : string) : Promise < any > {
get(tokenName : string) : Promise <TokenInfo> {

const promise = new Promise((resolve, reject) => {

Expand Down Expand Up @@ -103,9 +103,9 @@ constructor(private tokenConfig : any, private oauthConfig : OAuthConfig) {
* Otherwise, rejects.
*
* @param tokenName
* @returns {Promise<any>}
* @returns {Promise<TokenInfo>}
*/
refreshToken(tokenName : string) : Promise < any > {
refreshToken(tokenName : string) : Promise <TokenInfo> {

this._tokens[tokenName] = undefined;
return this.get(tokenName);
Expand All @@ -118,7 +118,7 @@ constructor(private tokenConfig : any, private oauthConfig : OAuthConfig) {
*
* @returns {Promise<T>}
*/
refreshAllTokens() : Promise < any > {
refreshAllTokens() : Promise <{ [key : string]: TokenInfo }> {

let refreshPromises = Object
.keys(this.tokenConfig)
Expand All @@ -139,7 +139,7 @@ constructor(private tokenConfig : any, private oauthConfig : OAuthConfig) {
* @param tokenName
* @returns {Promise<T>}
*/
private validateToken(tokenName : string) : Promise < any > {
private validateToken(tokenName : string) : Promise <TokenInfo> {

if (!this.tokenConfig[tokenName]) {
throw Error(`Token ${tokenName} does not exist.`);
Expand Down
6 changes: 3 additions & 3 deletions src/oauth-tooling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function createAuthCodeRequestUri(authorizationEndpoint: string, clientId: strin
* @returns {Promise<T>|Q.Promise<U>}
*/
function requestAccessToken(bodyObject: any, authorizationHeaderValue: string,
accessTokenEndpoint: string, realm: string): Promise<any> {
accessTokenEndpoint: string, realm: string): Promise<Token> {

const promise = new Promise(function(resolve, reject) {

Expand Down Expand Up @@ -95,7 +95,7 @@ function requestAccessToken(bodyObject: any, authorizationHeaderValue: string,
* @param accessToken
* @returns {Promise<T>}
*/
function getTokenInfo(tokenInfoUrl: string, accessToken: string): Promise<any> {
function getTokenInfo(tokenInfoUrl: string, accessToken: string): Promise<TokenInfo> {

const promise = new Promise(function(resolve, reject) {

Expand Down Expand Up @@ -152,7 +152,7 @@ function getTokenInfo(tokenInfoUrl: string, accessToken: string): Promise<any> {
* @param options
* @returns {Promise<T>}
*/
function getAccessToken(options: any): Promise<any> {
function getAccessToken(options: OAuthConfig): Promise<Token> {

validateOAuthConfig(options);

Expand Down
3 changes: 2 additions & 1 deletion src/types/OAuthConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ interface OAuthConfig {
accessTokenEndpoint: string;
tokenInfoEndpoint?: string;
realm: string; // (`SERVICES_REALM` | `EMPLOYEES_REALM`)
scopes?: string;
scopes?: string[];
redirect_uri?: string; // (required with `AUTHORIZATION_CODE_GRANT`)
code?: string; // (required with `AUTHORIZATION_CODE_GRANT`)
redirectUri?: string;
refreshToken?: string;
}
1 change: 0 additions & 1 deletion src/types/Token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
interface Token {
access_token: string;

expires_in: number;
id_token: string;
realm: string;
Expand Down
6 changes: 2 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ export function setTokeninfo(req: express.Request) {
uid,
scope,
cn,
/* tslint:disable */
expires_in,
/* tslint:enable */
expires_in, // tslint:disable-line
} = data;

const tokeninfo = {
Expand Down Expand Up @@ -141,7 +139,7 @@ export function rejectRequest(res: express.Response, status?: number) {
*
* @param options
*/
export function validateOAuthConfig(options: any) {
export function validateOAuthConfig(options: OAuthConfig) {

if (!options.credentialsDir) {
throw TypeError('credentialsDir must be defined');
Expand Down
13 changes: 0 additions & 13 deletions test/unit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ let expect = chai.expect;

describe('oauth tooling', () => {

let requestMock: any;
let responseMock: any;

before(() => {

requestMock = {};

responseMock = {};
responseMock.sendStatus = function(status: string) {
this.status = status;
};
});

describe('getAccessToken should throw TypeError', () => {

let config = {
Expand Down

0 comments on commit 6a44d09

Please sign in to comment.