From b3af12f558929aa1d05044994dc3afa75ddaa946 Mon Sep 17 00:00:00 2001 From: jorisdugue Date: Fri, 25 Aug 2023 15:09:31 +0200 Subject: [PATCH 1/3] * feat(chore): implement yandex configuration in preset * feat(chore): implement revoke token and revoke all token (#222) * feat(chore): update deps and implement gitlab provider * feat(docs): implement gitlab configuration in preset * feat(chore): implement revokeToken * feat(tests): implement missing test in interface * feat(chore,tests): implement revokeAllToken * feat(chore): implement revokeAllToken --------- Co-authored-by: joris --- README.md | 3 ++- examples/yandex.js | 33 +++++++++++++++++++++++++++++++++ index.js | 7 +++++++ test/index.test.js | 5 +++-- types/index.d.ts | 1 + types/index.test-d.ts | 1 + 6 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 examples/yandex.js diff --git a/README.md b/README.md index a7af6d4..73ba0e6 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/examples/yandex.js b/examples/yandex.js new file mode 100644 index 0000000..5d7b03a --- /dev/null +++ b/examples/yandex.js @@ -0,0 +1,33 @@ +const fastify = require('fastify')({ logger: { level: 'trace' } }) + +// 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}`) +}) diff --git a/index.js b/index.js index 8635512..1dc3cb5 100644 --- a/index.js +++ b/index.js @@ -322,6 +322,13 @@ fastifyOauth2.EPIC_GAMES_CONFIGURATION = { tokenPath: '/epic/oauth/v1/token' } +fastifyOauth2.YANDEX_CONFIGURATION = { + authorizeHost: 'https://oauth.yandex.com', + authorizePath: '/authorize', + tokenHost: 'https://oauth.yandex.com', + tokenPath: '/token' +} + module.exports = fp(fastifyOauth2, { fastify: '4.x', name: '@fastify/oauth2' diff --git a/test/index.test.js b/test/index.test.js index 126463d..f1bbd4a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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', @@ -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) { diff --git a/types/index.d.ts b/types/index.d.ts index 486aaf8..eef2b11 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -16,6 +16,7 @@ interface FastifyOauth2 extends FastifyPluginCallback(fastifyOauth2.TWITCH_CONFIGURATION); expectAssignable(fastifyOauth2.VATSIM_CONFIGURATION); expectAssignable(fastifyOauth2.VATSIM_DEV_CONFIGURATION); expectAssignable(fastifyOauth2.EPIC_GAMES_CONFIGURATION); +expectAssignable(fastifyOauth2.YANDEX_CONFIGURATION); server.get('/testOauth/callback', async (request, reply) => { expectType(server.testOAuthName); From a80eaea286f56b4651663a2d7a3ca5001ddab32f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=20=D0=9C=D0=B0=D0=BB=D0=BA?= =?UTF-8?q?=D0=BE?= Date: Sat, 26 Aug 2023 14:10:45 +0700 Subject: [PATCH 2/3] Add revoke token path for yandex --- index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1dc3cb5..2bd5e59 100644 --- a/index.js +++ b/index.js @@ -322,11 +322,15 @@ fastifyOauth2.EPIC_GAMES_CONFIGURATION = { tokenPath: '/epic/oauth/v1/token' } +/** + * Yandex ID docs https://yandex.ru/dev/id/doc/en/ + */ fastifyOauth2.YANDEX_CONFIGURATION = { authorizeHost: 'https://oauth.yandex.com', authorizePath: '/authorize', tokenHost: 'https://oauth.yandex.com', - tokenPath: '/token' + tokenPath: '/token', + revokePath: '/revoke_token' } module.exports = fp(fastifyOauth2, { From 96c56c4299a730db5e7a03fbe88b1e1db943aff1 Mon Sep 17 00:00:00 2001 From: Artem Malko Date: Sat, 26 Aug 2023 16:53:46 +0700 Subject: [PATCH 3/3] Update examples/yandex.js Co-authored-by: Uzlopak --- examples/yandex.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/yandex.js b/examples/yandex.js index 5d7b03a..cbc904f 100644 --- a/examples/yandex.js +++ b/examples/yandex.js @@ -1,3 +1,5 @@ +'use strict' + const fastify = require('fastify')({ logger: { level: 'trace' } }) // const oauthPlugin = require('fastify-oauth2')