Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
server
Manage
Activity
Members
Labels
Plan
Issues
25
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cokoapps
server
Commits
479cc603
Commit
479cc603
authored
1 year ago
by
Alexandros Georgantas
Committed by
Yannis Barlas
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
fix(server): correctly update existing oauth identities
parent
d96f5dc4
No related branches found
No related tags found
1 merge request
!76
fix(server): correctly update existing identities
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/models/identity/identity.controller.js
+29
-12
29 additions, 12 deletions
src/models/identity/identity.controller.js
src/models/identity/identity.resolvers.js
+6
-13
6 additions, 13 deletions
src/models/identity/identity.resolvers.js
with
35 additions
and
25 deletions
src/models/identity/identity.controller.js
+
29
−
12
View file @
479cc603
...
...
@@ -31,6 +31,17 @@ const getDefaultIdentity = async userId => {
}
}
const
hasValidRefreshToken
=
identity
=>
{
const
{
oauthRefreshTokenExpiration
,
oauthRefreshToken
}
=
identity
const
UTCNowTimestamp
=
moment
().
utc
().
toDate
().
getTime
()
return
(
!!
oauthRefreshToken
&&
!!
oauthRefreshTokenExpiration
&&
oauthRefreshTokenExpiration
.
getTime
()
>
UTCNowTimestamp
)
}
/**
* Authorise user OAuth.
* Save OAuth access and refresh tokens.
...
...
@@ -41,7 +52,7 @@ const createOAuthIdentity = async (userId, provider, sessionState, code) => {
try
{
let
identity
=
await
Identity
.
findOne
({
userId
,
provider
})
if
(
identity
)
{
if
(
identity
&&
hasValidRefreshToken
(
identity
)
)
{
return
identity
}
...
...
@@ -56,17 +67,22 @@ const createOAuthIdentity = async (userId, provider, sessionState, code) => {
Buffer
.
from
(
authData
.
oauthAccessToken
.
split
(
'
.
'
)[
1
],
'
base64
'
).
toString
(),
)
identity
=
await
Identity
.
insert
({
email
,
provider
,
userId
,
profileData
:
{
givenNames
,
surname
,
providerUserId
,
},
...
authData
,
})
if
(
!
identity
)
{
identity
=
await
Identity
.
insert
({
email
,
provider
,
userId
,
profileData
:
{
givenNames
,
surname
,
providerUserId
,
},
...
authData
,
})
}
else
{
identity
=
await
Identity
.
patchAndFetchById
(
identity
.
id
,
{
...
authData
})
}
const
{
oauthRefreshTokenExpiration
}
=
authData
const
expiresIn
=
(
oauthRefreshTokenExpiration
-
moment
().
utc
())
/
1000
...
...
@@ -140,4 +156,5 @@ module.exports = {
createOAuthIdentity
,
getUserIdentities
,
getDefaultIdentity
,
hasValidRefreshToken
,
}
This diff is collapsed.
Click to expand it.
src/models/identity/identity.resolvers.js
+
6
−
13
View file @
479cc603
const
logger
=
require
(
'
@pubsweet/logger
'
)
const
{
pubsubManager
}
=
require
(
'
pubsweet-server
'
)
const
moment
=
require
(
'
moment
'
)
const
{
createOAuthIdentity
}
=
require
(
'
./identity.controller
'
)
const
{
createOAuthIdentity
,
hasValidRefreshToken
,
}
=
require
(
'
./identity.controller
'
)
const
{
getUser
}
=
require
(
'
../user/user.controller
'
)
const
{
...
...
@@ -43,21 +46,11 @@ const createOAuthIdentityResolver = async (
}
}
const
hasValidRefreshTokenResolver
=
async
identity
=>
{
const
{
oauthRefreshTokenExpiration
}
=
identity
const
UTCNowTimestamp
=
moment
().
utc
().
toDate
().
getTime
()
return
(
oauthRefreshTokenExpiration
&&
oauthRefreshTokenExpiration
.
getTime
()
>
UTCNowTimestamp
)
}
module
.
exports
=
{
Mutation
:
{
createOAuthIdentity
:
createOAuthIdentityResolver
,
},
Identity
:
{
hasValidRefreshToken
:
hasValidRefreshTokenResolver
,
hasValidRefreshToken
,
},
}
This diff is collapsed.
Click to expand it.
Preview
0%
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