-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
uberf-8195: support openid auth (#6654)
Signed-off-by: Alexey Zinoviev <[email protected]>
- Loading branch information
Showing
11 changed files
with
196 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
plugins/login-resources/src/components/icons/OpenId.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<svg viewBox="0 0 512 512" width="1.5rem" height="1.5rem" xmlns="http://www.w3.org/2000/svg"> | ||
<rect | ||
height="512" | ||
rx="64" | ||
ry="64" | ||
style="fill:#f68423;fill-opacity:1;fill-rule:nonzero;stroke:none" | ||
width="512" | ||
x="0" | ||
y="0" | ||
/> | ||
<path | ||
d="m 416.99957,216.95084 c -39.2625,-22.18682 -78.9827,-34.64025 -117.0052,-39.52028 V 73.776502 l -60.66712,39.049158 v 63.02711 c -188.010218,14.68899 -295.068752,208.65924 0,262.37073 l 60.66712,-39.04916 V 218.28862 c 24.3468,4.22226 50.4759,12.17342 78.0094,24.69351 l -34.6758,21.70238 h 112.6719 v -73.76497 l -39.0003,26.0313 z m -177.67682,-1.66668 v 183.89018 c -182.841238,-23.72461 -140.809838,-171.94788 0,-183.89018 z" | ||
style="fill:#ffffff;fill-opacity:1" | ||
/> | ||
</svg> |
10 changes: 10 additions & 0 deletions
10
plugins/login-resources/src/components/providers/OpenId.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<script lang="ts"> | ||
import { Label } from '@hcengineering/ui' | ||
import OpenId from '../icons/OpenId.svelte' | ||
import login from '../../plugin' | ||
</script> | ||
|
||
<div class="flex-row-center flex-gap-2"> | ||
<OpenId /> | ||
<Label label={login.string.ContinueWith} params={{ provider: 'OpenId' }} /> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// | ||
// Copyright © 2024 Hardcore Engineering Inc. | ||
// | ||
// Licensed under the Eclipse Public License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. You may | ||
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// | ||
// See the License for the specific language governing permissions and | ||
// limitations under the f. | ||
// | ||
import { joinWithProvider, loginWithProvider, type LoginInfo } from '@hcengineering/account' | ||
import { BrandingMap, concatLink, MeasureContext, getBranding } from '@hcengineering/core' | ||
import Router from 'koa-router' | ||
import { Db } from 'mongodb' | ||
import { Issuer, Strategy } from 'openid-client' | ||
import qs from 'querystringify' | ||
|
||
import { Passport } from '.' | ||
import { getHost, safeParseAuthState } from './utils' | ||
|
||
export function registerOpenid ( | ||
measureCtx: MeasureContext, | ||
passport: Passport, | ||
router: Router<any, any>, | ||
accountsUrl: string, | ||
dbPromise: Promise<Db>, | ||
frontUrl: string, | ||
brandings: BrandingMap | ||
): string | undefined { | ||
const openidClientId = process.env.OPENID_CLIENT_ID | ||
const openidClientSecret = process.env.OPENID_CLIENT_SECRET | ||
const issuer = process.env.OPENID_ISSUER | ||
|
||
const redirectURL = '/auth/openid/callback' | ||
if (openidClientId === undefined || openidClientSecret === undefined || issuer === undefined) return | ||
|
||
void Issuer.discover(issuer).then((issuerObj) => { | ||
const client = new issuerObj.Client({ | ||
client_id: openidClientId, | ||
client_secret: openidClientSecret, | ||
redirect_uris: [concatLink(accountsUrl, redirectURL)], | ||
response_types: ['code'] | ||
}) | ||
|
||
passport.use( | ||
'oidc', | ||
new Strategy({ client, passReqToCallback: true }, (req: any, tokenSet: any, userinfo: any, done: any) => { | ||
return done(null, userinfo) | ||
}) | ||
) | ||
}) | ||
|
||
router.get('/auth/openid', async (ctx, next) => { | ||
measureCtx.info('try auth via', { provider: 'openid' }) | ||
const host = getHost(ctx.request.headers) | ||
const brandingKey = host !== undefined ? brandings[host]?.key ?? undefined : undefined | ||
const state = encodeURIComponent( | ||
JSON.stringify({ | ||
inviteId: ctx.query?.inviteId, | ||
branding: brandingKey | ||
}) | ||
) | ||
|
||
await passport.authenticate('oidc', { | ||
scope: 'openid profile email', | ||
state | ||
})(ctx, next) | ||
}) | ||
|
||
router.get( | ||
redirectURL, | ||
async (ctx, next) => { | ||
const state = safeParseAuthState(ctx.query?.state) | ||
const branding = getBranding(brandings, state?.branding) | ||
|
||
await passport.authenticate('oidc', { | ||
failureRedirect: concatLink(branding?.front ?? frontUrl, '/login') | ||
})(ctx, next) | ||
}, | ||
async (ctx, next) => { | ||
try { | ||
const email = ctx.state.user.email ?? `openid:${ctx.state.user.sub}` | ||
const [first, last] = ctx.state.user.name?.split(' ') ?? [ctx.state.user.username, ''] | ||
measureCtx.info('Provider auth handler', { email, type: 'openid' }) | ||
if (email !== undefined) { | ||
let loginInfo: LoginInfo | ||
const state = safeParseAuthState(ctx.query?.state) | ||
const branding = getBranding(brandings, state?.branding) | ||
const db = await dbPromise | ||
if (state.inviteId != null && state.inviteId !== '') { | ||
loginInfo = await joinWithProvider(measureCtx, db, null, email, first, last, state.inviteId as any, { | ||
openId: ctx.state.user.sub | ||
}) | ||
} else { | ||
loginInfo = await loginWithProvider(measureCtx, db, null, email, first, last, { | ||
openId: ctx.state.user.sub | ||
}) | ||
} | ||
|
||
const origin = concatLink(branding?.front ?? frontUrl, '/login/auth') | ||
const query = encodeURIComponent(qs.stringify({ token: loginInfo.token })) | ||
|
||
measureCtx.info('Success auth, redirect', { email, type: 'openid', target: origin }) | ||
// Successful authentication, redirect to your application | ||
ctx.redirect(`${origin}?${query}`) | ||
} | ||
} catch (err: any) { | ||
measureCtx.error('failed to auth', { err, type: 'openid', user: ctx.state?.user }) | ||
} | ||
await next() | ||
} | ||
) | ||
|
||
return 'openid' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters