Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dex
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
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
Code review analytics
Issue analytics
Insights
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
hdacloud
dex
Commits
bb985ca0
Unverified
Commit
bb985ca0
authored
4 months ago
by
Maksim Nabokikh
Committed by
GitHub
4 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Create offline sessions if approval is skipped (#3828)
Signed-off-by:
maksim.nabokikh
<
max.nabokih@gmail.com
>
parent
f1b711bb
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
server/handlers.go
+41
-48
41 additions, 48 deletions
server/handlers.go
server/handlers_test.go
+1
-1
1 addition, 1 deletion
server/handlers_test.go
with
42 additions
and
49 deletions
server/handlers.go
+
41
−
48
View file @
bb985ca0
...
...
@@ -534,23 +534,6 @@ func (s *Server) finalizeLogin(ctx context.Context, identity connector.Identity,
"connector_id"
,
authReq
.
ConnectorID
,
"username"
,
claims
.
Username
,
"preferred_username"
,
claims
.
PreferredUsername
,
"email"
,
email
,
"groups"
,
claims
.
Groups
)
// we can skip the redirect to /approval and go ahead and send code if it's not required
if
s
.
skipApproval
&&
!
authReq
.
ForceApprovalPrompt
{
return
""
,
true
,
nil
}
// an HMAC is used here to ensure that the request ID is unpredictable, ensuring that an attacker who intercepted the original
// flow would be unable to poll for the result at the /approval endpoint
h
:=
hmac
.
New
(
sha256
.
New
,
authReq
.
HMACKey
)
h
.
Write
([]
byte
(
authReq
.
ID
))
mac
:=
h
.
Sum
(
nil
)
returnURL
:=
path
.
Join
(
s
.
issuerURL
.
Path
,
"/approval"
)
+
"?req="
+
authReq
.
ID
+
"&hmac="
+
base64
.
RawURLEncoding
.
EncodeToString
(
mac
)
_
,
ok
:=
conn
.
(
connector
.
RefreshConnector
)
if
!
ok
{
return
returnURL
,
false
,
nil
}
offlineAccessRequested
:=
false
for
_
,
scope
:=
range
authReq
.
Scopes
{
if
scope
==
scopeOfflineAccess
{
...
...
@@ -558,45 +541,55 @@ func (s *Server) finalizeLogin(ctx context.Context, identity connector.Identity,
break
}
}
if
!
offlineAccessRequested
{
return
returnURL
,
false
,
nil
}
_
,
canRefresh
:=
conn
.
(
connector
.
RefreshConnector
)
// Try to retrieve an existing OfflineSession object for the corresponding user.
session
,
err
:=
s
.
storage
.
GetOfflineSessions
(
identity
.
UserID
,
authReq
.
ConnectorID
)
if
err
!=
nil
{
if
err
!=
storage
.
ErrNotFound
{
s
.
logger
.
ErrorContext
(
ctx
,
"failed to get offline session"
,
"err"
,
err
)
return
""
,
false
,
err
}
offlineSessions
:=
storage
.
OfflineSessions
{
UserID
:
identity
.
UserID
,
ConnID
:
authReq
.
ConnectorID
,
Refresh
:
make
(
map
[
string
]
*
storage
.
RefreshTokenRef
),
ConnectorData
:
identity
.
ConnectorData
,
}
if
offlineAccessRequested
&&
canRefresh
{
// Try to retrieve an existing OfflineSession object for the corresponding user.
session
,
err
:=
s
.
storage
.
GetOfflineSessions
(
identity
.
UserID
,
authReq
.
ConnectorID
)
switch
{
case
err
!=
nil
&&
err
==
storage
.
ErrNotFound
:
offlineSessions
:=
storage
.
OfflineSessions
{
UserID
:
identity
.
UserID
,
ConnID
:
authReq
.
ConnectorID
,
Refresh
:
make
(
map
[
string
]
*
storage
.
RefreshTokenRef
),
ConnectorData
:
identity
.
ConnectorData
,
}
// Create a new OfflineSession object for the user and add a reference object for
// the newly received refreshtoken.
if
err
:=
s
.
storage
.
CreateOfflineSessions
(
ctx
,
offlineSessions
);
err
!=
nil
{
s
.
logger
.
ErrorContext
(
ctx
,
"failed to create offline session"
,
"err"
,
err
)
// Create a new OfflineSession object for the user and add a reference object for
// the newly received refreshtoken.
if
err
:=
s
.
storage
.
CreateOfflineSessions
(
ctx
,
offlineSessions
);
err
!=
nil
{
s
.
logger
.
ErrorContext
(
ctx
,
"failed to create offline session"
,
"err"
,
err
)
return
""
,
false
,
err
}
case
err
==
nil
:
// Update existing OfflineSession obj with new RefreshTokenRef.
if
err
:=
s
.
storage
.
UpdateOfflineSessions
(
session
.
UserID
,
session
.
ConnID
,
func
(
old
storage
.
OfflineSessions
)
(
storage
.
OfflineSessions
,
error
)
{
if
len
(
identity
.
ConnectorData
)
>
0
{
old
.
ConnectorData
=
identity
.
ConnectorData
}
return
old
,
nil
});
err
!=
nil
{
s
.
logger
.
ErrorContext
(
ctx
,
"failed to update offline session"
,
"err"
,
err
)
return
""
,
false
,
err
}
default
:
s
.
logger
.
ErrorContext
(
ctx
,
"failed to get offline session"
,
"err"
,
err
)
return
""
,
false
,
err
}
return
returnURL
,
false
,
nil
}
// Update existing OfflineSession obj with new RefreshTokenRef.
if
err
:=
s
.
storage
.
UpdateOfflineSessions
(
session
.
UserID
,
session
.
ConnID
,
func
(
old
storage
.
OfflineSessions
)
(
storage
.
OfflineSessions
,
error
)
{
if
len
(
identity
.
ConnectorData
)
>
0
{
old
.
ConnectorData
=
identity
.
ConnectorData
}
return
old
,
nil
});
err
!=
nil
{
s
.
logger
.
ErrorContext
(
ctx
,
"failed to update offline session"
,
"err"
,
err
)
return
""
,
false
,
err
// we can skip the redirect to /approval and go ahead and send code if it's not required
if
s
.
skipApproval
&&
!
authReq
.
ForceApprovalPrompt
{
return
""
,
true
,
nil
}
// an HMAC is used here to ensure that the request ID is unpredictable, ensuring that an attacker who intercepted the original
// flow would be unable to poll for the result at the /approval endpoint
h
:=
hmac
.
New
(
sha256
.
New
,
authReq
.
HMACKey
)
h
.
Write
([]
byte
(
authReq
.
ID
))
mac
:=
h
.
Sum
(
nil
)
returnURL
:=
path
.
Join
(
s
.
issuerURL
.
Path
,
"/approval"
)
+
"?req="
+
authReq
.
ID
+
"&hmac="
+
base64
.
RawURLEncoding
.
EncodeToString
(
mac
)
return
returnURL
,
false
,
nil
}
...
...
This diff is collapsed.
Click to expand it.
server/handlers_test.go
+
1
−
1
View file @
bb985ca0
...
...
@@ -519,7 +519,7 @@ func TestHandlePasswordLoginWithSkipApproval(t *testing.T) {
Scopes
:
[]
string
{
"offline_access"
},
},
expectedRes
:
"/auth/mockPw/cb"
,
offlineSessionCreated
:
fals
e
,
offlineSessionCreated
:
tru
e
,
},
}
...
...
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