Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MetaGer Keymanager
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open-source
MetaGer Keymanager
Commits
8c96305b
Commit
8c96305b
authored
2 years ago
by
Dominik Hebeler
Browse files
Options
Downloads
Patches
Plain Diff
finished oidc connect integration with keycloak
parent
825f8068
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pass/routes/authentication.js
+41
-39
41 additions, 39 deletions
pass/routes/authentication.js
with
41 additions
and
39 deletions
pass/routes/authentication.js
+
41
−
39
View file @
8c96305b
...
@@ -2,72 +2,74 @@ var express = require("express");
...
@@ -2,72 +2,74 @@ var express = require("express");
var
router
=
express
.
Router
();
var
router
=
express
.
Router
();
const
config
=
require
(
"
config
"
);
const
config
=
require
(
"
config
"
);
const
jose
=
require
(
"
jose
"
);
const
jose
=
require
(
"
jose
"
);
const
{
auth
,
requiresAuth
,
Session
}
=
require
(
"
express-openid-connect
"
);
const
{
auth
,
requiresAuth
,
claimCheck
}
=
require
(
"
express-openid-connect
"
);
const
dayjs
=
require
(
"
dayjs
"
);
const
auth_session_expiration_minutes
=
1
;
const
auth_session_expiration_seconds
=
60
;
const
access_keys_redis_key
=
"
accesskeys
"
;
/**
/**
* Authorization is done using Oauth2 via any
GitLab Instance
* Authorization is done using Oauth2 via any
Openid Provider
* The Instance to be used can be configured in the config.
* The Instance to be used can be configured in the config.
*
*
* The authenticated user needs to be member of the group defined in the config
*
* You can call any url with the parameter moderation=true to trigger a login with
* You can call any url with the parameter moderation=true to trigger a login with
* redirect to the same URL after successfull authentication or 401 error on
* redirect to the same URL after successfull authentication or 401 error on
* failed authentication.
* failed authentication.
*
* Create an application in your Gitlab Instances Group with openid,profile,email permissions
* and set the callback URL to this applications base url and the path /callback
*
*/
*/
router
.
use
(
router
.
use
(
// General Optional authorization for every page
// It will not require login or start a session by default
// Only when a route uses the middleware requireAuth
auth
({
auth
({
issuerBaseURL
:
`
${
config
.
get
(
"
app.openid_auth.url
"
)}
`
,
issuerBaseURL
:
`
${
config
.
get
(
"
app.openid_auth.url
"
)}
`
,
baseURL
:
config
.
get
(
"
app.url
"
),
baseURL
:
config
.
get
(
"
app.url
"
),
clientID
:
config
.
get
(
"
app.openid_auth.app_id
"
),
clientID
:
config
.
get
(
"
app.openid_auth.app_id
"
),
clientSecret
:
config
.
get
(
"
app.openid_auth.app_secret
"
),
clientSecret
:
config
.
get
(
"
app.openid_auth.app_secret
"
),
secret
:
config
.
get
(
"
app.secret
"
),
secret
:
config
.
get
(
"
app.secret
"
),
idpLogout
:
true
,
session
:
{
session
:
{
absoluteDuration
:
1
,
rollingDuration
:
auth_session_expiration_seconds
,
},
signSessionStoreCookie
:
true
,
afterCallback
:
(
req
,
res
,
session
,
decodedState
)
=>
{
let
access_token
=
session
.
access_token
;
console
.
log
(
session
);
return
session
;
},
},
authRequired
:
false
,
authRequired
:
false
,
authorizationParams
:
{
response_type
:
"
code
"
,
},
}),
}),
/**
* If the user is already authenticated this middleware will check
* if the user inherits the required role in his oidc token to use this app
*/
(
req
,
res
,
next
)
=>
{
(
req
,
res
,
next
)
=>
{
if
(
req
.
query
.
moderation
)
{
if
(
req
.
data
===
undefined
)
{
req
uiresAuth
()(
req
,
res
,
next
)
;
req
.
data
=
{
admin
:
false
}
;
}
else
{
}
else
{
next
(
"
route
"
);
req
.
data
.
admin
=
false
;
}
if
(
req
.
oidc
.
isAuthenticated
())
{
claimCheck
((
req
,
claims
)
=>
{
if
(
req
.
oidc
.
isAuthenticated
())
{
if
(
claims
.
resource_access
!==
undefined
&&
claims
.
resource_access
[
"
metager-key
"
]
!==
undefined
&&
claims
.
resource_access
[
"
metager-key
"
].
roles
!==
undefined
&&
claims
.
resource_access
[
"
metager-key
"
].
roles
.
includes
(
"
metager-key
"
)
)
{
req
.
data
.
admin
=
true
;
}
return
true
;
}
})(
req
,
res
,
next
);
}
else
{
next
();
}
}
},
},
// Require authorization for every route with moderation request parameter set
// Or when the user already is authenticated
(
req
,
res
,
next
)
=>
{
(
req
,
res
,
next
)
=>
{
if
(
req
.
data
)
{
if
(
req
.
query
.
moderation
||
req
.
data
.
admin
)
{
req
.
data
.
admin
=
false
;
req
uiresAuth
()(
req
,
res
,
next
)
;
}
else
{
}
else
{
req
.
data
=
{
next
(
"
route
"
);
admin
:
false
,
};
}
if
(
Object
.
keys
(
req
.
appSession
).
length
>
0
)
{
let
claims
=
jose
.
decodeJwt
(
req
.
appSession
.
id_token
);
if
(
claims
.
groups_direct
.
includes
(
config
.
get
(
"
app.gitlab.required_group
"
))
)
{
req
.
data
.
admin
=
true
;
}
}
}
},
// Redirect the user to the same URL without moderation URL Parameter after
// successful auth. Show him a 401 if he does not inherit required role
(
req
,
res
,
next
)
=>
{
if
(
req
.
query
.
moderation
)
{
if
(
req
.
query
.
moderation
)
{
if
(
!
req
.
data
.
admin
)
{
if
(
!
req
.
data
.
admin
)
{
res
.
locals
.
error
=
{
status
:
401
};
res
.
locals
.
error
=
{
status
:
401
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment