Skip to content

Commit 02d64cd

Browse files
authored
Merge pull request #942 from SISheogorath/feature/openID
Add OpenID to CodiMD
2 parents 32af96a + 9f9c408 commit 02d64cd

File tree

8 files changed

+91
-4
lines changed

8 files changed

+91
-4
lines changed

lib/config/default.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,6 @@ module.exports = {
145145
email: true,
146146
allowEmailRegister: true,
147147
allowGravatar: true,
148-
allowPDFExport: true
148+
allowPDFExport: true,
149+
openID: true
149150
}

lib/config/environment.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,6 @@ module.exports = {
123123
email: toBooleanConfig(process.env.CMD_EMAIL),
124124
allowEmailRegister: toBooleanConfig(process.env.CMD_ALLOW_EMAIL_REGISTER),
125125
allowGravatar: toBooleanConfig(process.env.CMD_ALLOW_GRAVATAR),
126-
allowPDFExport: toBooleanConfig(process.env.CMD_ALLOW_PDF_EXPORT)
126+
allowPDFExport: toBooleanConfig(process.env.CMD_ALLOW_PDF_EXPORT),
127+
openID: toBooleanConfig(process.env.CMD_OPENID)
127128
}

lib/config/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ config.isGoogleEnable = config.google.clientID && config.google.clientSecret
9696
config.isDropboxEnable = config.dropbox.clientID && config.dropbox.clientSecret
9797
config.isTwitterEnable = config.twitter.consumerKey && config.twitter.consumerSecret
9898
config.isEmailEnable = config.email
99+
config.isOpenIDEnable = config.openID
99100
config.isGitHubEnable = config.github.clientID && config.github.clientSecret
100101
config.isGitLabEnable = config.gitlab.clientID && config.gitlab.clientSecret
101102
config.isMattermostEnable = config.mattermost.clientID && config.mattermost.clientSecret

lib/response.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ function showIndex (req, res, next) {
8888
email: config.isEmailEnable,
8989
allowEmailRegister: config.allowEmailRegister,
9090
allowPDFExport: config.allowPDFExport,
91+
openID: config.isOpenIDEnable,
9192
signin: authStatus,
9293
infoMessage: req.flash('info'),
9394
errorMessage: req.flash('error'),
@@ -142,7 +143,8 @@ function responseCodiMD (res, note) {
142143
oauth2: config.isOAuth2Enable,
143144
email: config.isEmailEnable,
144145
allowEmailRegister: config.allowEmailRegister,
145-
allowPDFExport: config.allowPDFExport
146+
allowPDFExport: config.allowPDFExport,
147+
openID: config.isOpenIDEnable
146148
})
147149
}
148150

lib/web/auth/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ if (config.isLDAPEnable) authRouter.use(require('./ldap'))
4545
if (config.isSAMLEnable) authRouter.use(require('./saml'))
4646
if (config.isOAuth2Enable) authRouter.use(require('./oauth2'))
4747
if (config.isEmailEnable) authRouter.use(require('./email'))
48+
if (config.isOpenIDEnable) authRouter.use(require('./openid'))
4849

4950
// logout
5051
authRouter.get('/logout', function (req, res) {

lib/web/auth/openid/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict'
2+
3+
const Router = require('express').Router
4+
const passport = require('passport')
5+
const OpenIDStrategy = require('@passport-next/passport-openid').Strategy
6+
const config = require('../../../config')
7+
const models = require('../../../models')
8+
const logger = require('../../../logger')
9+
const {urlencodedParser} = require('../../utils')
10+
const {setReturnToFromReferer} = require('../utils')
11+
12+
let openIDAuth = module.exports = Router()
13+
14+
passport.use(new OpenIDStrategy({
15+
returnURL: config.serverURL + '/auth/openid/callback',
16+
realm: config.serverURL,
17+
profile: true
18+
}, function (openid, profile, done) {
19+
var stringifiedProfile = JSON.stringify(profile)
20+
models.User.findOrCreate({
21+
where: {
22+
profileid: openid
23+
},
24+
defaults: {
25+
profile: stringifiedProfile
26+
}
27+
}).spread(function (user, created) {
28+
if (user) {
29+
var needSave = false
30+
if (user.profile !== stringifiedProfile) {
31+
user.profile = stringifiedProfile
32+
needSave = true
33+
}
34+
if (needSave) {
35+
user.save().then(function () {
36+
if (config.debug) { logger.info('user login: ' + user.id) }
37+
return done(null, user)
38+
})
39+
} else {
40+
if (config.debug) { logger.info('user login: ' + user.id) }
41+
return done(null, user)
42+
}
43+
}
44+
}).catch(function (err) {
45+
logger.error('auth callback failed: ' + err)
46+
return done(err, null)
47+
})
48+
}))
49+
50+
openIDAuth.post('/auth/openid', urlencodedParser, function (req, res, next) {
51+
setReturnToFromReferer(req)
52+
passport.authenticate('openid')(req, res, next)
53+
})
54+
55+
// openID auth callback
56+
openIDAuth.get('/auth/openid/callback',
57+
passport.authenticate('openid', {
58+
successReturnToOrRedirect: config.serverurl + '/',
59+
failureRedirect: config.serverurl + '/'
60+
})
61+
)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"doctoc": "doctoc --title='# Table of Contents' README.md"
1616
},
1717
"dependencies": {
18+
"@passport-next/passport-openid": "^1.0.0",
1819
"Idle.Js": "git+https://github.com/shawnmclean/Idle.js",
1920
"archiver": "^2.1.1",
2021
"async": "^2.1.4",

public/views/shared/signin-modal.ejs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,26 @@
7878
</div>
7979
</form>
8080
<% } %>
81-
<% if((facebook || twitter || github || gitlab || mattermost || dropbox || google || ldap || oauth2) && email) { %>
81+
<% if((facebook || twitter || github || gitlab || mattermost || dropbox || google || ldap || oauth2) && openID) { %>
82+
<hr>
83+
<% }%>
84+
<% if(openID) { %>
85+
<h4>OpenID</h4>
86+
<form data-toggle="validator" role="form" class="form-horizontal" method="post" enctype="application/x-www-form-urlencoded">
87+
<div class="form-group">
88+
<div class="col-sm-12">
89+
<input type="text" class="form-control" name="openid_identifier" placeholder="OpenID" required>
90+
<span class="help-block control-label with-errors" style="display: inline;"></span>
91+
</div>
92+
</div>
93+
<div class="form-group">
94+
<div class="col-sm-12">
95+
<button type="submit" class="btn btn-primary" formaction="<%- url %>/auth/openid">Sign in</button>
96+
</div>
97+
</div>
98+
</form>
99+
<% } %>
100+
<% if((facebook || twitter || github || gitlab || mattermost || dropbox || google || ldap || oauth2 || openID) && email) { %>
82101
<hr>
83102
<% }%>
84103
<% if(email) { %>

0 commit comments

Comments
 (0)