@@ -34,12 +34,23 @@ func (s *Server) getElevate(c *gin.Context) {
3434 return
3535 }
3636
37+ primaryWorkflow := request .Workflow
38+
39+ if len (primaryWorkflow ) == 0 {
40+ if len (role .Workflows ) == 0 {
41+ s .getErrorPage (c , http .StatusBadRequest , "No workflow specified and role has no associated workflows" , err )
42+ return
43+ }
44+ primaryWorkflow = role .Workflows [0 ]
45+ }
46+
3747 s .elevate (c , models.ElevateRequest {
38- Role : role ,
39- Provider : request .Provider ,
40- Reason : request .Reason ,
41- Duration : request .Duration ,
42- Session : request .Session ,
48+ Role : role ,
49+ Providers : []string {request .Provider },
50+ Workflow : primaryWorkflow ,
51+ Reason : request .Reason ,
52+ Duration : request .Duration ,
53+ Session : request .Session ,
4354 })
4455}
4556
@@ -129,7 +140,7 @@ func (s *Server) handleDynamicRequest(c *gin.Context, dynamicRequest models.Elev
129140 dynamicRole := & models.Role {
130141 Name : "dynamic-role-" + time .Now ().Format ("20060102-150405" ),
131142 Description : "Dynamically created role: " + dynamicRequest .Reason ,
132- Workflow : dynamicRequest .Workflow ,
143+ Workflows : [] string { dynamicRequest .Workflow } ,
133144 Permissions : models.Permissions {
134145 Allow : dynamicRequest .Permissions ,
135146 },
@@ -147,11 +158,12 @@ func (s *Server) handleDynamicRequest(c *gin.Context, dynamicRequest models.Elev
147158
148159 // Convert to standard ElevateRequest
149160 elevateRequest := models.ElevateRequest {
150- Role : dynamicRole ,
151- Provider : dynamicRequest .Providers [0 ], // Use first provider for now
152- Reason : dynamicRequest .Reason ,
153- Duration : dynamicRequest .Duration ,
154- Session : nil , // Session will be handled by the workflow if needed
161+ Role : dynamicRole ,
162+ Providers : dynamicRequest .Providers , // Use first provider for now
163+ Workflow : dynamicRequest .Workflow ,
164+ Reason : dynamicRequest .Reason ,
165+ Duration : dynamicRequest .Duration ,
166+ Session : nil , // Session will be handled by the workflow if needed
155167 }
156168
157169 s .elevate (c , elevateRequest )
@@ -164,7 +176,36 @@ func (s *Server) elevate(c *gin.Context, request models.ElevateRequest) {
164176
165177 ctx := context .Background ()
166178
167- WorkflowTask , err := s .Workflows .CreateWorkflow (ctx , request )
179+ // If we have a web session and one hasn't been set then
180+ // lets attach a user session to the request.
181+ if s .Config .IsServer () {
182+
183+ // Get the auth provider from the workflow if set
184+ authProvider := []string {}
185+
186+ if len (request .Workflow ) > 0 {
187+ workflowDef , err := s .Config .GetWorkflowByName (request .Workflow )
188+ if err != nil {
189+ s .getErrorPage (c , http .StatusBadRequest , "Invalid workflow specified" , err )
190+ return
191+ }
192+ authProvider = []string {workflowDef .GetAuthentication ()}
193+ }
194+
195+ foundUser , err := s .getUser (c , authProvider ... )
196+
197+ if err != nil {
198+ s .getErrorPage (c , http .StatusUnauthorized , "Unauthorized: unable to get user for list of available roles" , err )
199+ return
200+ }
201+
202+ if foundUser != nil {
203+ request .Session = foundUser .ToLocalSession (s .Config .GetServices ().GetEncryption ())
204+ }
205+
206+ }
207+
208+ workflowTask , err := s .Workflows .CreateWorkflow (ctx , request )
168209
169210 if err != nil {
170211 s .getErrorPage (c , http .StatusBadRequest , "Failed to execute workflow" , err )
@@ -173,7 +214,7 @@ func (s *Server) elevate(c *gin.Context, request models.ElevateRequest) {
173214
174215 // We now redirect the user to the next workflow step.
175216 c .Redirect (http .StatusTemporaryRedirect ,
176- WorkflowTask .GetRedirectURL (),
217+ workflowTask .GetRedirectURL (),
177218 )
178219}
179220
@@ -289,6 +330,13 @@ func (s *Server) getElevateAuthOAuth2(c *gin.Context) {
289330
290331 workflowTask .SetUser (session .User )
291332
333+ localSession := session .ToLocalSession (s .Config .GetServices ().GetEncryption ())
334+
335+ if err := s .setAuthCookie (c , authProvider , localSession ); err != nil {
336+ s .getErrorPage (c , http .StatusInternalServerError , "Failed to set auth cookie" , err )
337+ return
338+ }
339+
292340 s .resumeWorkflow (c , workflowTask )
293341
294342}
0 commit comments