Skip to content

Commit

Permalink
Adds API Key authentification method (#118)
Browse files Browse the repository at this point in the history
* Adds API Key authentification method

* Makes header name configurable
  • Loading branch information
richfab authored Jul 12, 2023
1 parent 83a22f5 commit 29efce7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
23 changes: 23 additions & 0 deletions gbfs-validator/__test__/__snapshots__/gbfs.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,26 @@ GBFS {
"url": "http://localhost:8888/gbfs.json",
}
`;

exports[`initialization with auth should correctly initialize with \`api_key\` 1`] = `
GBFS {
"auth": Object {
"apiKey": Object {
"key": "mykey",
"value": "myvalue",
},
"type": "api_key",
},
"gotOptions": Object {
"headers": Object {
"mykey": "myvalue",
},
},
"options": Object {
"docked": false,
"freefloating": false,
"version": null,
},
"url": "http://localhost:8888/gbfs.json",
}
`;
14 changes: 14 additions & 0 deletions gbfs-validator/__test__/gbfs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ describe('initialization', () => {
})
).toMatchSnapshot()
})

test('should correctly initialize with `api_key`', () => {
expect(
new GBFS('http://localhost:8888/gbfs.json', {
auth: {
type: 'api_key',
apiKey: {
key: 'mykey',
value: 'myvalue'
}
}
})
).toMatchSnapshot()
})
})
})

Expand Down
6 changes: 6 additions & 0 deletions gbfs-validator/gbfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ class GBFS {
Authorization: `Bearer ${this.auth.bearerToken.token}`
}
}

if (this.auth.type === 'api_key' && this.auth.apiKey) {
this.gotOptions.headers = {
[this.auth.apiKey.key]: `${this.auth.apiKey.value}`
}
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions website/src/pages/Validator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const state = reactive({
type: null,
basicAuth: { user: null, password: null },
bearerToken: { token: null },
apiKey: { key: null },
oauthClientCredentialsGrant: {
user: null,
password: null,
Expand Down Expand Up @@ -45,6 +46,10 @@ const state = reactive({
value: 'bearer_token',
text: 'Bearer Token'
},
{
value: 'api_key',
text: 'API Key'
},
{
value: 'oauth_client_credentials_grant',
text: 'Oauth Client Credentials Grant'
Expand Down Expand Up @@ -196,6 +201,30 @@ function updateURL() {
></b-form-input>
</b-form-group>

<b-form-group
id="input-group-api_key"
label="Authentification"
label-for="input-api_key"
v-if="state.options.auth.type === 'api_key'"
>
<b-row>
<b-col>
<b-form-input
id="input-api_key-key"
placeholder="key"
v-model="state.options.auth.apiKey.key"
></b-form-input>
</b-col>
<b-col>
<b-form-input
id="input-api_key-value"
placeholder="value"
v-model="state.options.auth.apiKey.value"
></b-form-input>
</b-col>
</b-row>
</b-form-group>

<b-form-group
id="input-group-oauth_client_credentials_grant"
label="Authentification"
Expand Down

0 comments on commit 29efce7

Please sign in to comment.