From d5dd27f74524e2a20dd5e8c939e0ca67c45e58f5 Mon Sep 17 00:00:00 2001 From: YusukeIwaki Date: Sat, 28 Dec 2019 22:20:18 +0900 Subject: [PATCH 1/4] Implement bitbucket login. Signed-off-by: YusukeIwaki --- app.js | 1 + lib/config/environment.js | 4 ++++ lib/config/index.js | 1 + lib/web/auth/bitbucket/index.js | 30 ++++++++++++++++++++++++++++ lib/web/auth/index.js | 1 + package.json | 1 + public/views/index/body.ejs | 4 ++-- public/views/shared/signin-modal.ejs | 11 +++++++--- yarn.lock | 12 +++++++++-- 9 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 lib/web/auth/bitbucket/index.js diff --git a/app.js b/app.js index 4093bd3b..9acd94ca 100644 --- a/app.js +++ b/app.js @@ -186,6 +186,7 @@ app.locals.authProviders = { facebook: config.isFacebookEnable, twitter: config.isTwitterEnable, github: config.isGitHubEnable, + bitbucket: config.isBitbucketEnable, gitlab: config.isGitLabEnable, mattermost: config.isMattermostEnable, dropbox: config.isDropboxEnable, diff --git a/lib/config/environment.js b/lib/config/environment.js index 71b1b984..3a62985c 100644 --- a/lib/config/environment.js +++ b/lib/config/environment.js @@ -70,6 +70,10 @@ module.exports = { clientID: process.env.CMD_GITHUB_CLIENTID, clientSecret: process.env.CMD_GITHUB_CLIENTSECRET }, + bitbucket: { + clientID: process.env.CMD_BITBUCKET_CLIENTID, + clientSecret: process.env.CMD_BITBUCKET_CLIENTSECRET + }, gitlab: { baseURL: process.env.CMD_GITLAB_BASEURL, clientID: process.env.CMD_GITLAB_CLIENTID, diff --git a/lib/config/index.js b/lib/config/index.js index b422da74..325746f8 100644 --- a/lib/config/index.js +++ b/lib/config/index.js @@ -121,6 +121,7 @@ config.isTwitterEnable = config.twitter.consumerKey && config.twitter.consumerSe config.isEmailEnable = config.email config.isOpenIDEnable = config.openID config.isGitHubEnable = config.github.clientID && config.github.clientSecret +config.isBitbucketEnable = config.bitbucket.clientID && config.bitbucket.clientSecret config.isGitLabEnable = config.gitlab.clientID && config.gitlab.clientSecret config.isMattermostEnable = config.mattermost.clientID && config.mattermost.clientSecret config.isLDAPEnable = config.ldap.url diff --git a/lib/web/auth/bitbucket/index.js b/lib/web/auth/bitbucket/index.js new file mode 100644 index 00000000..f8648748 --- /dev/null +++ b/lib/web/auth/bitbucket/index.js @@ -0,0 +1,30 @@ +'use strict' + +const Router = require('express').Router +const passport = require('passport') +const BitbucketStrategy = require('passport-bitbucket-oauth2').Strategy +const config = require('../../../config') +const response = require('../../../response') +const { setReturnToFromReferer, passportGeneralCallback } = require('../utils') +const { URL } = require('url') + +const bitbucketAuth = module.exports = Router() + +passport.use(new BitbucketStrategy({ + clientID: config.bitbucket.clientID, + clientSecret: config.bitbucket.clientSecret, + callbackURL: config.serverURL + '/auth/bitbucket/callback', +}, passportGeneralCallback)) + +bitbucketAuth.get('/auth/bitbucket', function (req, res, next) { + setReturnToFromReferer(req) + passport.authenticate('bitbucket')(req, res, next) +}) + +// bitbucket auth callback +bitbucketAuth.get('/auth/bitbucket/callback', + passport.authenticate('bitbucket', { + successReturnToOrRedirect: config.serverURL + '/', + failureRedirect: config.serverURL + '/' + }) +) diff --git a/lib/web/auth/index.js b/lib/web/auth/index.js index 86ab4b28..b387b710 100644 --- a/lib/web/auth/index.js +++ b/lib/web/auth/index.js @@ -37,6 +37,7 @@ passport.deserializeUser(function (id, done) { if (config.isFacebookEnable) authRouter.use(require('./facebook')) if (config.isTwitterEnable) authRouter.use(require('./twitter')) if (config.isGitHubEnable) authRouter.use(require('./github')) +if (config.isBitbucketEnable) authRouter.use(require('./bitbucket')) if (config.isGitLabEnable) authRouter.use(require('./gitlab')) if (config.isMattermostEnable) authRouter.use(require('./mattermost')) if (config.isDropboxEnable) authRouter.use(require('./dropbox')) diff --git a/package.json b/package.json index 6c82bfc4..ff679bcb 100644 --- a/package.json +++ b/package.json @@ -110,6 +110,7 @@ "passport-dropbox-oauth2": "~1.1.0", "passport-facebook": "~2.1.1", "passport-github": "~1.1.0", + "passport-bitbucket-oauth2": "~0.1.2", "passport-gitlab2": "~4.0.0", "passport-google-oauth20": "~1.0.0", "passport-ldapauth": "~2.1.3", diff --git a/public/views/index/body.ejs b/public/views/index/body.ejs index a5eb0b49..7b520070 100644 --- a/public/views/index/body.ejs +++ b/public/views/index/body.ejs @@ -15,7 +15,7 @@ <% if (allowAnonymous) { %> <%= __('New guest note') %> <% } %> - <% if (authProviders.facebook || authProviders.twitter || authProviders.github || authProviders.gitlab || authProviders.mattermost || authProviders.dropbox || authProviders.google || authProviders.ldap || authProviders.saml || authProviders.oauth2 || authProviders.email) { %> + <% if (authProviders.facebook || authProviders.twitter || authProviders.github || authProviders.bitbucket || authProviders.gitlab ||authProviders.mattermost || authProviders.dropbox || authProviders.google || authProviders.ldap || authProviders.saml || authProviders.oauth2 || authProviders.email) { %> <% } %> @@ -50,7 +50,7 @@ <% if (errorMessage && errorMessage.length > 0) { %>
<%= errorMessage %>
<% } %> - <% if (authProviders.facebook || authProviders.twitter || authProviders.github || authProviders.gitlab || authProviders.mattermost || authProviders.dropbox || authProviders.google || authProviders.ldap || authProviders.saml || authProviders.oauth2 || authProviders.email) { %> + <% if (authProviders.facebook || authProviders.twitter || authProviders.github|| authProviders.bitbucket || authProviders.gitlab || authProviders.mattermost || authProviders.dropbox || authProviders.google || authProviders.ldap || authProviders.saml || authProviders.oauth2 || authProviders.email) { %>