Skip to content

Commit a73dd68

Browse files
authored
feat(chore): implement yandex configuration in preset (#228)
1 parent 250b891 commit a73dd68

File tree

6 files changed

+53
-3
lines changed

6 files changed

+53
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ You can choose some default setup to assign to `auth` option.
9191
- `VATSIM_CONFIGURATION`
9292
- `VATSIM_DEV_CONFIGURATION`
9393
- `EPIC_GAMES_CONFIGURATION`
94+
- `YANDEX_CONFIGURATION`
9495

9596
### Custom configuration
9697

@@ -303,7 +304,7 @@ declare module 'fastify' {
303304

304305
## Provider Quirks
305306

306-
The following providers require additional work to be set up correctly.
307+
The following providers require additional work to be set up correctly.
307308

308309
### Twitch
309310

examples/yandex.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
const fastify = require('fastify')({ logger: { level: 'trace' } })
4+
5+
// const oauthPlugin = require('fastify-oauth2')
6+
const oauthPlugin = require('..')
7+
8+
fastify.register(oauthPlugin, {
9+
name: 'yandexOAuth2',
10+
scope: ['login:email'],
11+
credentials: {
12+
client: {
13+
id: process.env.CLIENT_ID,
14+
secret: process.env.CLIENT_SECRET
15+
},
16+
auth: oauthPlugin.YANDEX_CONFIGURATION
17+
},
18+
startRedirectPath: '/login/yandex',
19+
callbackUri: `http://localhost:${process.env.PORT}/login/yandex/callback`
20+
})
21+
22+
fastify.get('/login/yandex/callback', async (req, reply) => {
23+
const token = await fastify.yandexOAuth2.getAccessTokenFromAuthorizationCodeFlow(req)
24+
25+
console.log(token)
26+
reply.send({ access_token: token.access_token })
27+
})
28+
29+
fastify.listen(process.env.PORT, (err, address) => {
30+
if (err) {
31+
fastify.log.error(err)
32+
process.exit(1)
33+
}
34+
fastify.log.info(`server listening on ${address}`)
35+
})

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,17 @@ fastifyOauth2.EPIC_GAMES_CONFIGURATION = {
325325
tokenPath: '/epic/oauth/v1/token'
326326
}
327327

328+
/**
329+
* Yandex ID docs https://yandex.ru/dev/id/doc/en/
330+
*/
331+
fastifyOauth2.YANDEX_CONFIGURATION = {
332+
authorizeHost: 'https://oauth.yandex.com',
333+
authorizePath: '/authorize',
334+
tokenHost: 'https://oauth.yandex.com',
335+
tokenPath: '/token',
336+
revokePath: '/revoke_token'
337+
}
338+
328339
module.exports = fp(fastifyOauth2, {
329340
fastify: '4.x',
330341
name: '@fastify/oauth2'

test/index.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ t.test('preset configuration generate-callback-uri-params', t => {
800800
})
801801

802802
t.test('preset configuration generate-callback-uri-params', t => {
803-
t.plan(52)
803+
t.plan(56)
804804

805805
const presetConfigs = [
806806
'FACEBOOK_CONFIGURATION',
@@ -815,7 +815,8 @@ t.test('preset configuration generate-callback-uri-params', t => {
815815
'TWITCH_CONFIGURATION',
816816
'VATSIM_CONFIGURATION',
817817
'VATSIM_DEV_CONFIGURATION',
818-
'EPIC_GAMES_CONFIGURATION'
818+
'EPIC_GAMES_CONFIGURATION',
819+
'YANDEX_CONFIGURATION'
819820
]
820821

821822
for (const configName of presetConfigs) {

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface FastifyOauth2 extends FastifyPluginCallback<fastifyOauth2.FastifyOAuth
1616
VATSIM_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
1717
VATSIM_DEV_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
1818
EPIC_GAMES_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
19+
YANDEX_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
1920
}
2021

2122
declare namespace fastifyOauth2 {

types/index.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ expectAssignable<ProviderConfiguration>(fastifyOauth2.TWITCH_CONFIGURATION);
101101
expectAssignable<ProviderConfiguration>(fastifyOauth2.VATSIM_CONFIGURATION);
102102
expectAssignable<ProviderConfiguration>(fastifyOauth2.VATSIM_DEV_CONFIGURATION);
103103
expectAssignable<ProviderConfiguration>(fastifyOauth2.EPIC_GAMES_CONFIGURATION);
104+
expectAssignable<ProviderConfiguration>(fastifyOauth2.YANDEX_CONFIGURATION);
104105

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

0 commit comments

Comments
 (0)