Skip to content

Commit

Permalink
41 type information (#42)
Browse files Browse the repository at this point in the history
feat(types): Added Types
close #41
  • Loading branch information
Retro64 authored Apr 19, 2017
1 parent d222a02 commit 8423689
Show file tree
Hide file tree
Showing 21 changed files with 561 additions and 526 deletions.
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,33 @@ import {
} from 'lib-oauth-tooling';
```

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

Class to request and cache tokens on client-side.

```typescript
let tokenCache = new TokenCache({
const tokenCache = new TokenCache({
'service-foo': ['foo.read', 'foo.write'],
'service-bar': ['bar.read']
}, oAuthConfig);

tokenCache.get('service-foo')
.then((tokeninfo) => {
console.log(tokeninfo.access_token);
});
.then((tokeninfo) => {
console.log(tokeninfo.access_token);
});
```

`oauthConfig`:
* `credentialsDir` string
* `grantType` string (`AUTHORIZATION_CODE_GRANT` | `PASSWORD_CREDENTIALS_GRANT`)
* `accessTokenEndpoint` string
* `tokenInfoEndpoint` string
* `tokenInfoEndpoint` string - mandatory for TokenCache
* `realm` string (`SERVICES_REALM` | `EMPLOYEES_REALM`)
* `scopes` string optional
* `redirect_uri` string optional (required with `AUTHORIZATION_CODE_GRANT`)
* `code` string optional (required with `AUTHORIZATION_CODE_GRANT`)

#### handleOAuthRequestMiddleware(options: any)
#### handleOAuthRequestMiddleware(options: MiddlewareOptions)

Express middleware to extract and validate an access token. It attaches the scopes matched by the token to the request (`request.scopes`) for further usage.
If the token is not valid the request is rejected (with 401 Unauthorized).
Expand Down Expand Up @@ -84,32 +84,32 @@ 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`.
```typescript
getTokenInfo(tokenInfoEndpoint, accessToken)
.then((tokeninfo) => {
console.log(tokeninfo.access_token);
})
.catch((err) => {
console.log(err);
});
.then((tokeninfo) => {
console.log(tokeninfo.access_token);
})
.catch((err) => {
console.log(err);
});
```
#### getAccessToken(options: any)
#### getAccessToken(options: OAuthConfig)
Helper function to get an access token for the specified scopes.
```typescript
getAccessToken(options)
.then((accessToken) => {
console.log(accessToken);
})
.catch((err) => {
console.log(err);
});
.then((accessToken) => {
console.log(accessToken);
})
.catch((err) => {
console.log(err);
});
```
`options`:
Expand Down Expand Up @@ -147,7 +147,7 @@ String constant specifying the employees realm.
If you want to test oAuth locally without being able to actually call real endpoints this library provides some tooling.
#### mockTokenInfoEndpoint(options: any)
#### mockTokenInfoEndpoint(options: MockOptions)
Mocks a `tokeninfo` endpoint.
Expand All @@ -167,7 +167,7 @@ mockTokeninfoEndpoint({
* `tokens` any optional (list of valid tokens)
* `times` number optional (for how many times/calls the endpoint is mocked, default is `Number.MAX_SAFE_INTEGER`)
#### mockAccessTokenEndpoint(options: any)
#### mockAccessTokenEndpoint(options: MockOptions)
Mocks a `access_token` endpoint.
Expand Down
68 changes: 34 additions & 34 deletions integration-test/mock-tooling/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('mock tooling', () => {
});

// when
let promise = getTokenInfo(tokeninfoEndpoint, 'invalid');
const promise = getTokenInfo(tokeninfoEndpoint, 'invalid');

// then
return expect(promise).to.rejected;
Expand All @@ -60,7 +60,7 @@ describe('mock tooling', () => {
});

// when
let promise = getTokenInfo(tokeninfoEndpoint, 'foo');
const promise = getTokenInfo(tokeninfoEndpoint, 'foo');

// then
return expect(promise).to.become(validAuthToken);
Expand All @@ -82,22 +82,22 @@ describe('mock tooling', () => {
});

// when
let promise = getTokenInfo(tokeninfoEndpoint, 'foo')
.then((token: any) => {
const promise = getTokenInfo(tokeninfoEndpoint, 'foo')
.then((token: TokenInfo) => {

expect(token).to.equal(validAuthToken);
return getTokenInfo(tokeninfoEndpoint, 'foo');
})
.then((token: any) => {
expect(token).to.equal(validAuthToken);
return getTokenInfo(tokeninfoEndpoint, 'foo');
})
.then((token: TokenInfo) => {

expect(token).to.equal(validAuthToken);
return getTokenInfo(tokeninfoEndpoint, 'foo');
})
.then((token: any) => {
expect(token).to.equal(validAuthToken);
return getTokenInfo(tokeninfoEndpoint, 'foo');
})
.then((token: TokenInfo) => {

expect(token).to.equal(validAuthToken);
return getTokenInfo(tokeninfoEndpoint, 'foo');
});
expect(token).to.equal(validAuthToken);
return getTokenInfo(tokeninfoEndpoint, 'foo');
});

// then
return expect(promise).to.rejected;
Expand All @@ -119,25 +119,25 @@ describe('mock tooling', () => {

// when
return getTokenInfo(tokeninfoEndpoint, 'foo')
.then((token: any) => {
.then((token: TokenInfo) => {

expect(token).to.deep.equal(validAuthToken);
return getTokenInfo(tokeninfoEndpoint, 'foo');
})
.then((token: any) => {
expect(token).to.deep.equal(validAuthToken);
return getTokenInfo(tokeninfoEndpoint, 'foo');
})
.then((token: TokenInfo) => {

expect(token).to.deep.equal(validAuthToken);
return getTokenInfo(tokeninfoEndpoint, 'foo');
})
.then((token: any) => {
expect(token).to.deep.equal(validAuthToken);
return getTokenInfo(tokeninfoEndpoint, 'foo');
})
.then((token: TokenInfo) => {

expect(token).to.deep.equal(validAuthToken);
return getTokenInfo(tokeninfoEndpoint, 'foo');
})
.then((token: any) => {
expect(token).to.deep.equal(validAuthToken);
return getTokenInfo(tokeninfoEndpoint, 'foo');
})
.then((token: TokenInfo) => {

expect(token).to.deep.equal(validAuthToken);
});
expect(token).to.deep.equal(validAuthToken);
});
});

});
Expand Down Expand Up @@ -166,11 +166,11 @@ describe('mock tooling', () => {
});

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

return getTokenInfo(tokeninfoEndpoint, token.access_token);
});
return getTokenInfo(tokeninfoEndpoint, token.access_token);
});

// then
return expect(promise).to.eventually.haveOwnProperty('access_token');
Expand Down
82 changes: 41 additions & 41 deletions integration-test/oauth-tooling/getAccessToken.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function setupTestEnvironment(authHeader: string, authServerApp: Express.Applica
authServerApp.use(bodyParser.urlencoded({extended: true}));
authServerApp.post('/oauth2/access_token', function(req, res) {
if (req.body.grant_type === PASSWORD_CREDENTIALS_GRANT) {
let valid = req.headers['authorization'] === authHeader;
const valid = req.headers['authorization'] === authHeader;
if (valid) {
res
.status(HttpStatus.OK)
Expand All @@ -46,7 +46,7 @@ function setupTestEnvironment(authHeader: string, authServerApp: Express.Applica
.status(HttpStatus.UNAUTHORIZED)
.send({
error: 'internal_error',
error_description : 'Request method GET not supported'
error_description: 'Request method GET not supported'
});
}
}
Expand All @@ -59,7 +59,7 @@ describe('getAccessToken', () => {
let authenticationServer: Http.Server;
let authServerApp: Express.Application;

let getAccessTokenOptions;
let getAccessTokenOptions: OAuthConfig;

// Setup AuthServer
beforeEach(() => {
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('getAccessToken', () => {
setupTestEnvironment('Basic c3R1cHNfY2FtcC1mcm9udGVuZF80NTgxOGFkZC1jNDdkLTQ3MzEtYTQwZC1jZWExZmZkMGUwYzk6Nmk1Z2hCI1MyaUJLKSVidGI3JU14Z3hRWDcxUXIuKSo=', authServerApp);

//when
let promise = getAccessToken(getAccessTokenOptions);
const promise = getAccessToken(getAccessTokenOptions);

//then
return expect(promise).to.become({access_token: '4b70510f-be1d-4f0f-b4cb-edbca2c79d41'});
Expand All @@ -103,7 +103,7 @@ describe('getAccessToken', () => {
setupTestEnvironment('invalid', authServerApp);

//when
let promise = getAccessToken(getAccessTokenOptions);
const promise = getAccessToken(getAccessTokenOptions);

//then
return expect(promise).to.be.rejected;
Expand All @@ -115,7 +115,7 @@ describe('getAccessToken', () => {
setupTestEnvironment('invalid', authServerApp);

//when
let promise = getAccessToken(Object.assign({}, getAccessTokenOptions, {
const promise = getAccessToken(Object.assign({}, getAccessTokenOptions, {
credentialsDir: 'integration-test/data/not-existing'
}));

Expand All @@ -127,7 +127,7 @@ describe('getAccessToken', () => {

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

let getAccessTokenOptionsAuthorization;
let getAccessTokenOptionsAuthorization: OAuthConfig​​ ;

before(() => {
getAccessTokenOptionsAuthorization = {
Expand All @@ -147,10 +147,10 @@ describe('getAccessToken', () => {
setupTestEnvironment('Basic c3R1cHNfY2FtcC1mcm9udGVuZF80NTgxOGFkZC1jNDdkLTQ3MzEtYTQwZC1jZWExZmZkMGUwYzk6Nmk1Z2hCI1MyaUJLKSVidGI3JU14Z3hRWDcxUXIuKSo=', authServerApp);

//when
let bearer = getAccessToken(getAccessTokenOptionsAuthorization)
.then((data) => {
return data;
});
const bearer = getAccessToken(getAccessTokenOptionsAuthorization)
.then((data) => {
return data;
});

//then
return expect(bearer).to.become({access_token: '4b70510f-be1d-4f0f-b4cb-edbca2c79d41'});
Expand All @@ -162,7 +162,7 @@ describe('getAccessToken', () => {
setupTestEnvironment('invalid', authServerApp);

//when
let promise = getAccessToken(getAccessTokenOptionsAuthorization);
const promise = getAccessToken(getAccessTokenOptionsAuthorization);

//then
return expect(promise).to.be.rejected;
Expand All @@ -174,7 +174,7 @@ describe('getAccessToken', () => {
setupTestEnvironment('invalid', authServerApp);

//when
let promise = getAccessToken(Object.assign({}, getAccessTokenOptionsAuthorization, {
const promise = getAccessToken(Object.assign({}, getAccessTokenOptionsAuthorization, {
credentialsDir: 'integration-test/data/not-existing'
}));

Expand All @@ -199,30 +199,30 @@ describe('getAccessToken', () => {
const responseObject = { 'access_token': '4b70510f-be1d-4f0f-b4cb-edbca2c79d41' };

nock(host)
.post('/access_token?realm=/services', (body) => {
.post('/access_token?realm=/services', (body: any) => {

if (body.grant_type !== options.grantType) {
return false;
}
if (body.grant_type !== options.grantType) {
return false;
}

if (body.scope !== options.scopes.join(' ')) {
return false;
}
if (body.scope !== options.scopes.join(' ')) {
return false;
}

if (body.redirect_uri !== options.redirectUri) {
return false;
}
if (body.redirect_uri !== options.redirectUri) {
return false;
}

if (body.code !== options.code) {
return false;
}
if (body.code !== options.code) {
return false;
}

return true;
})
.reply(HttpStatus.OK, responseObject);
return true;
})
.reply(HttpStatus.OK, responseObject);

// when
let promise = getAccessToken(options);
const promise = getAccessToken(options);

// then
return expect(promise).to.become(responseObject);
Expand All @@ -246,22 +246,22 @@ describe('getAccessToken', () => {
const responseObject = { 'access_token': '4b70510f-be1d-4f0f-b4cb-edbca2c79d41' };

nock(host)
.post('/access_token?realm=/employees', (body) => {
.post('/access_token?realm=/employees', (body: any) => {

if (body.grant_type !== options.grantType) {
return false;
}
if (body.grant_type !== options.grantType) {
return false;
}

if (body.refresh_token !== options.refreshToken) {
return false;
}
if (body.refresh_token !== options.refreshToken) {
return false;
}

return true;
})
.reply(HttpStatus.OK, responseObject);
return true;
})
.reply(HttpStatus.OK, responseObject);

// when
let promise = getAccessToken(options);
const promise = getAccessToken(options);

// then
return expect(promise).to.become(responseObject);
Expand Down
Loading

0 comments on commit 8423689

Please sign in to comment.