Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(chore): implement yandex configuration in preset #228

Merged
merged 3 commits into from
Aug 26, 2023
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ You can choose some default setup to assign to `auth` option.
- `VATSIM_CONFIGURATION`
- `VATSIM_DEV_CONFIGURATION`
- `EPIC_GAMES_CONFIGURATION`
- `YANDEX_CONFIGURATION`

### Custom configuration

Expand Down Expand Up @@ -303,7 +304,7 @@ declare module 'fastify' {

## Provider Quirks

The following providers require additional work to be set up correctly.
The following providers require additional work to be set up correctly.

### Twitch

Expand Down
35 changes: 35 additions & 0 deletions examples/yandex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const fastify = require('fastify')({ logger: { level: 'trace' } })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fastify/plugins I think the examples/ folder is becoming too big to ship

I would switch to a single generic example and add a custom one only if necessary

Not necessary to do it in this PR tho 👍🏼

artem-malko marked this conversation as resolved.
Show resolved Hide resolved

// const oauthPlugin = require('fastify-oauth2')
const oauthPlugin = require('..')

fastify.register(oauthPlugin, {
name: 'yandexOAuth2',
scope: ['login:email'],
credentials: {
client: {
id: process.env.CLIENT_ID,
secret: process.env.CLIENT_SECRET
},
auth: oauthPlugin.YANDEX_CONFIGURATION
},
startRedirectPath: '/login/yandex',
callbackUri: `http://localhost:${process.env.PORT}/login/yandex/callback`
})

fastify.get('/login/yandex/callback', async (req, reply) => {
const token = await fastify.yandexOAuth2.getAccessTokenFromAuthorizationCodeFlow(req)

console.log(token)
reply.send({ access_token: token.access_token })
})

fastify.listen(process.env.PORT, (err, address) => {
if (err) {
fastify.log.error(err)
process.exit(1)
}
fastify.log.info(`server listening on ${address}`)
})
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,17 @@ fastifyOauth2.EPIC_GAMES_CONFIGURATION = {
tokenPath: '/epic/oauth/v1/token'
}

/**
* Yandex ID docs https://yandex.ru/dev/id/doc/en/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good. I also thought that the lack of references is bad. Thank you for adding the reference.

*/
fastifyOauth2.YANDEX_CONFIGURATION = {
authorizeHost: 'https://oauth.yandex.com',
authorizePath: '/authorize',
tokenHost: 'https://oauth.yandex.com',
tokenPath: '/token',
revokePath: '/revoke_token'
}

module.exports = fp(fastifyOauth2, {
fastify: '4.x',
name: '@fastify/oauth2'
Expand Down
5 changes: 3 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ t.test('preset configuration generate-callback-uri-params', t => {
})

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

const presetConfigs = [
'FACEBOOK_CONFIGURATION',
Expand All @@ -815,7 +815,8 @@ t.test('preset configuration generate-callback-uri-params', t => {
'TWITCH_CONFIGURATION',
'VATSIM_CONFIGURATION',
'VATSIM_DEV_CONFIGURATION',
'EPIC_GAMES_CONFIGURATION'
'EPIC_GAMES_CONFIGURATION',
'YANDEX_CONFIGURATION'
]

for (const configName of presetConfigs) {
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface FastifyOauth2 extends FastifyPluginCallback<fastifyOauth2.FastifyOAuth
VATSIM_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
VATSIM_DEV_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
EPIC_GAMES_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
YANDEX_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
}

declare namespace fastifyOauth2 {
Expand Down
1 change: 1 addition & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ expectAssignable<ProviderConfiguration>(fastifyOauth2.TWITCH_CONFIGURATION);
expectAssignable<ProviderConfiguration>(fastifyOauth2.VATSIM_CONFIGURATION);
expectAssignable<ProviderConfiguration>(fastifyOauth2.VATSIM_DEV_CONFIGURATION);
expectAssignable<ProviderConfiguration>(fastifyOauth2.EPIC_GAMES_CONFIGURATION);
expectAssignable<ProviderConfiguration>(fastifyOauth2.YANDEX_CONFIGURATION);

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