Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Add support for bearer auth #216

Merged
merged 3 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions example/swagger-files/auth-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,6 @@
]
}
},
"/anything/oauth2-another": {
"post": {
"summary": "Oauth2 security type",
"description": "",
"parameters": [],
"responses": {},
"security": [
{
"oauth2": []
}
]
}
},
"/anything/basic": {
"post": {
"summary": "Basic security type",
Expand All @@ -49,15 +36,15 @@
]
}
},
"/anything/basic-another": {
"/anything/bearer": {
"post": {
"summary": "Basic security type",
"summary": "Bearer security type",
"description": "",
"parameters": [],
"responses": {},
"security": [
{
"basic": []
"bearer": []
}
]
}
Expand Down Expand Up @@ -85,6 +72,10 @@
"type": "http",
"scheme": "basic"
},
"bearer": {
"type": "http",
"scheme": "bearer"
},
"apiKey": {
"type": "apiKey",
"in": "query",
Expand Down
13 changes: 12 additions & 1 deletion packages/api-explorer/__tests__/lib/get-auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ it('should return apiKey property for apiKey', () => {
expect(getSingle(topLevelUser, { type: 'oauth2' })).toBe('123456');
});

it('should return apiKey property for bearer', () => {
expect(getSingle(topLevelUser, { type: 'http', scheme: 'bearer' })).toBe('123456');
});

it('should return user/pass properties for basic auth', () => {
expect(getSingle(topLevelUser, { type: 'http', scheme: 'basic' })).toEqual({
user: 'user',
Expand All @@ -73,11 +77,16 @@ it('should return selected app from keys array if app provided', () => {

it('should return item by scheme name if no apiKey/user/pass', () => {
expect(getSingle(topLevelSchemeUser, { type: 'oauth2', _key: 'schemeName' })).toBe('scheme-key');
expect(
getSingle(topLevelSchemeUser, { type: 'http', scheme: 'bearer', _key: 'schemeName' }),
).toBe('scheme-key');
expect(getSingle(keysSchemeUser, { type: 'oauth2', _key: 'schemeName' })).toBe('scheme-key-1');
expect(getSingle(keysSchemeUser, { type: 'oauth2', _key: 'schemeName' }, 'app-2')).toBe(
'scheme-key-2',
);
expect(getSingle(keysSchemeUser, { type: 'http', _key: 'schemeName' }, 'app-3')).toEqual({
expect(
getSingle(keysSchemeUser, { type: 'http', scheme: 'basic', _key: 'schemeName' }, 'app-3'),
).toEqual({
user: 'user',
pass: 'pass',
});
Expand All @@ -86,6 +95,8 @@ it('should return item by scheme name if no apiKey/user/pass', () => {
it('should return emptystring for anything else', () => {
expect(getSingle(topLevelUser, { type: 'unknown' })).toBe('');
expect(getSingle({}, { type: 'http', scheme: 'basic' })).toEqual({ user: '', pass: '' });
expect(getSingle({}, { type: 'http', scheme: 'bearer' })).toEqual('');
expect(getSingle({}, { type: 'http', scheme: 'unknown' })).toEqual('');
expect(getSingle(keysUser, { type: 'unknown' })).toBe('');
expect(getSingle(keysUser, { type: 'unknown' }, 'app-2')).toBe('');
});
Expand Down
9 changes: 8 additions & 1 deletion packages/api-explorer/src/lib/get-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ function getKey(user, scheme) {
case 'apiKey':
return user[scheme._key] || user.apiKey || '';
case 'http':
return user[scheme._key] || { user: user.user || '', pass: user.pass || '' };
if (scheme.scheme === 'basic') {
return user[scheme._key] || { user: user.user || '', pass: user.pass || '' };
}

if (scheme.scheme === 'bearer') {
return user[scheme._key] || user.apiKey || '';
}
return '';
default:
return '';
}
Expand Down