From d2172e51790e6269d418b7133d9202f4e0bba533 Mon Sep 17 00:00:00 2001 From: Miguel Serrano Date: Thu, 27 Apr 2023 12:22:29 +0200 Subject: [PATCH] [web] Issue Oauth2 access tokens (#12788) * [web] Issue Oauth2 access tokens * [web] Add partial index for `oauthAccessTokens.user_id` for Personal Access Tokens * [web] script to create personal access tokens GitOrigin-RevId: 796e8d23a6799a87ac6096c686139c6290668b83 --- .../web/app/src/models/OauthAccessToken.js | 4 +++ ...095212_personal_oauth_tokens_user_index.js | 21 ++++++++++++ .../create_oauth_personal_access_token.js | 33 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 services/web/migrations/20230426095212_personal_oauth_tokens_user_index.js create mode 100644 services/web/scripts/create_oauth_personal_access_token.js diff --git a/services/web/app/src/models/OauthAccessToken.js b/services/web/app/src/models/OauthAccessToken.js index aa54352a63..8b3dbc8926 100644 --- a/services/web/app/src/models/OauthAccessToken.js +++ b/services/web/app/src/models/OauthAccessToken.js @@ -6,13 +6,17 @@ const { ObjectId } = Schema const OauthAccessTokenSchema = new Schema( { accessToken: String, + accessTokenPartial: String, + type: String, accessTokenExpiresAt: Date, oauthApplication_id: { type: ObjectId, ref: 'OauthApplication' }, refreshToken: String, refreshTokenExpiresAt: Date, scope: String, user_id: { type: ObjectId, ref: 'User' }, + createdAt: { type: Date }, expiresAt: Date, + lastUsedAt: Date, }, { collection: 'oauthAccessTokens', diff --git a/services/web/migrations/20230426095212_personal_oauth_tokens_user_index.js b/services/web/migrations/20230426095212_personal_oauth_tokens_user_index.js new file mode 100644 index 0000000000..68ecf045eb --- /dev/null +++ b/services/web/migrations/20230426095212_personal_oauth_tokens_user_index.js @@ -0,0 +1,21 @@ +const Helpers = require('./lib/helpers') + +exports.tags = ['server-ce', 'server-pro', 'saas'] + +const indexes = [ + { + key: { user_id: 1 }, + name: 'pat_user_id_1', + partialFilterExpression: { type: 'pat' }, + }, +] + +exports.migrate = async client => { + const { db } = client + await Helpers.addIndexesToCollection(db.oauthAccessTokens, indexes) +} + +exports.rollback = async client => { + const { db } = client + await Helpers.dropIndexesFromCollection(db.oauthAccessTokens, indexes) +} diff --git a/services/web/scripts/create_oauth_personal_access_token.js b/services/web/scripts/create_oauth_personal_access_token.js new file mode 100644 index 0000000000..418cd27de9 --- /dev/null +++ b/services/web/scripts/create_oauth_personal_access_token.js @@ -0,0 +1,33 @@ +// Script to create a Personal Access Token for a given user +// Example: +// node scripts/create_oauth_personal_access_token.js --user-id=643e5b240dc50c83b5bf1127 + +const parseArgs = require('minimist') +const { waitForDb } = require('../app/src/infrastructure/mongodb') +const OAuthPersonalAccessTokenManager = require('../modules/oauth2-server/app/src/OAuthPersonalAccessTokenManager') + +const argv = parseArgs(process.argv.slice(2), { + string: ['user-id'], +}) + +const userId = argv['user-id'] + +if (!userId) { + console.error('Missing --user-id argument') + process.exit(1) +} + +async function createPersonalAccessToken() { + await waitForDb() + const accessToken = await OAuthPersonalAccessTokenManager.createToken(userId) + console.log('Personal Access Token: ' + accessToken) +} + +createPersonalAccessToken() + .then(() => { + process.exit() + }) + .catch(err => { + console.error(err) + process.exit(1) + })