Skip to content

Commit 2a6d98a

Browse files
feat: add vatsim configuration setup (#135)
* Update index.d.ts * Update README.md * Update index.js * Update index.d.ts * Create vatsim.js * Create vatsim-dev.js * Update index.test-d.ts
1 parent 14b162c commit 2a6d98a

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ You can choose some default setup to assign to `auth` option.
6464
- `SPOTIFY_CONFIGURATION`
6565
- `DISCORD_CONFIGURATION`
6666
- `TWITCH_CONFIGURATION`
67+
- `VATSIM_CONFIGURATION`
68+
- `VATSIM_DEV_CONFIGURATION`
6769

6870
### Custom configuration
6971

examples/vatsim-dev.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict'
2+
3+
const fastify = require('fastify')({ logger: { level: 'trace' } })
4+
const sget = require('simple-get')
5+
6+
// const oauthPlugin = require('fastify-oauth2')
7+
const oauthPlugin = require('..')
8+
9+
fastify.register(oauthPlugin, {
10+
name: 'vatsimoauthdev',
11+
scope: ['full_name', 'email', 'vatsim_details', 'country'],
12+
credentials: {
13+
client: {
14+
id: '<CLIENT_ID>',
15+
secret: '<CLIENT_SECRET>'
16+
},
17+
auth: oauthPlugin.VATSIM_DEV_CONFIGURATION
18+
},
19+
startRedirectPath: '/login/vatsim',
20+
callbackUri: 'http://localhost:3000/login/vatsim/callback'
21+
})
22+
23+
fastify.get('/login/vatsim/callback', function (request, reply) {
24+
this.vatsimoauthdev.getAccessTokenFromAuthorizationCodeFlow(
25+
request,
26+
(err, result) => {
27+
if (err) {
28+
reply.send(err)
29+
return
30+
}
31+
32+
sget.concat(
33+
{
34+
url: 'https://auth-dev.vatsim.net/api/user',
35+
method: 'GET',
36+
headers: {
37+
Authorization: 'Bearer ' + result.access_token
38+
},
39+
json: true
40+
},
41+
function (err, res, data) {
42+
if (err) {
43+
reply.send(err)
44+
return
45+
}
46+
reply.send(data)
47+
}
48+
)
49+
}
50+
)
51+
})
52+
53+
fastify.listen(3000)

examples/vatsim.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict'
2+
3+
const fastify = require('fastify')({ logger: { level: 'trace' } })
4+
const sget = require('fastify')
5+
6+
// const oauthPlugin = require('fastify-oauth2')
7+
const oauthPlugin = require('..')
8+
9+
fastify.register(oauthPlugin, {
10+
name: 'vatsimoauth',
11+
scope: ['full_name', 'email', 'vatsim_details', 'country'],
12+
credentials: {
13+
client: {
14+
id: '<CLIENT_ID>',
15+
secret: '<CLIENT_SECRET>'
16+
},
17+
auth: oauthPlugin.VATSIM_CONFIGURATION
18+
},
19+
startRedirectPath: '/login/vatsim',
20+
callbackUri: 'http://localhost:3000/login/vatsim/callback'
21+
})
22+
23+
fastify.get('/login/vatsim/callback', function (request, reply) {
24+
this.vatsimoauthdev.getAccessTokenFromAuthorizationCodeFlow(
25+
request,
26+
(err, result) => {
27+
if (err) {
28+
reply.send(err)
29+
return
30+
}
31+
32+
sget.concat(
33+
{
34+
url: 'https://auth.vatsim.net/api/user',
35+
method: 'GET',
36+
headers: {
37+
Authorization: 'Bearer ' + result.access_token
38+
},
39+
json: true
40+
},
41+
function (err, res, data) {
42+
if (err) {
43+
reply.send(err)
44+
return
45+
}
46+
reply.send(data)
47+
}
48+
)
49+
}
50+
)
51+
})
52+
53+
fastify.listen(3000)

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ interface FastifyOAuth2 {
1111
SPOTIFY_CONFIGURATION: ProviderConfiguration;
1212
VKONTAKTE_CONFIGURATION: ProviderConfiguration;
1313
TWITCH_CONFIGURATION: ProviderConfiguration;
14+
VATSIM_CONFIGURATION: ProviderConfiguration;
15+
VATSIM_DEV_CONFIGURATION: ProviderConfiguration;
1416
}
1517

1618
export const fastifyOauth2: FastifyPluginCallback<FastifyOAuth2Options> & FastifyOAuth2

index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,18 @@ oauthPlugin.TWITCH_CONFIGURATION = {
237237
tokenPath: '/oauth2/token'
238238
}
239239

240+
oauthPlugin.VATSIM_CONFIGURATION = {
241+
authorizeHost: 'https://auth.vatsim.net',
242+
authorizePath: '/oauth/authorize',
243+
tokenHost: 'https://auth.vatsim.net',
244+
tokenPath: '/oauth/token'
245+
}
246+
247+
oauthPlugin.VATSIM_DEV_CONFIGURATION = {
248+
authorizeHost: 'https://auth-dev.vatsim.net',
249+
authorizePath: '/oauth/authorize',
250+
tokenHost: 'https://auth-dev.vatsim.net',
251+
tokenPath: '/oauth/token'
252+
}
253+
240254
module.exports = oauthPlugin

test/types/index.test-d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ expectAssignable<ProviderConfiguration>(fastifyOauth2.MICROSOFT_CONFIGURATION);
5757
expectAssignable<ProviderConfiguration>(fastifyOauth2.SPOTIFY_CONFIGURATION);
5858
expectAssignable<ProviderConfiguration>(fastifyOauth2.VKONTAKTE_CONFIGURATION);
5959
expectAssignable<ProviderConfiguration>(fastifyOauth2.TWITCH_CONFIGURATION);
60+
expectAssignable<ProviderConfiguration>(fastifyOauth2.VATSIM_CONFIGURATION);
61+
expectAssignable<ProviderConfiguration>(fastifyOauth2.VATSIM_DEV_CONFIGURATION);
6062

6163
server.get('/testOauth/callback', async request => {
6264
expectType<OAuth2Namespace>(server.testOAuthName);

0 commit comments

Comments
 (0)