@@ -16,14 +16,18 @@ package authn
16
16
17
17
import (
18
18
"context"
19
+ "fmt"
19
20
"github.com/greenpau/go-authcrunch/pkg/requests"
20
21
"github.com/greenpau/go-authcrunch/pkg/user"
21
22
"go.uber.org/zap"
22
23
"net/http"
23
24
"strings"
24
25
)
25
26
26
- func (p * Portal ) handleHTTPAppsAwsSso (ctx context.Context , w http.ResponseWriter , r * http.Request , rr * requests.Request , parsedUser * user.User ) error {
27
+ func (p * Portal ) handleHTTPAppsSingleSignOn (ctx context.Context , w http.ResponseWriter , r * http.Request , rr * requests.Request , parsedUser * user.User ) error {
28
+ var assumedRole , authorizedRole bool
29
+ var accountID , roleName string
30
+
27
31
p .disableClientCache (w )
28
32
p .injectRedirectURL (ctx , w , r , rr )
29
33
@@ -54,6 +58,30 @@ func (p *Portal) handleHTTPAppsAwsSso(ctx context.Context, w http.ResponseWriter
54
58
resp .PageTitle = "AWS SSO"
55
59
resp .BaseURL (rr .Upstream .BasePath )
56
60
61
+ if strings .Contains (r .URL .Path , "/apps/sso" ) && strings .Contains (r .URL .Path , "metadata.xml" ) {
62
+ // TODO(greenpau): add metadata for realm.
63
+ }
64
+
65
+ if strings .Contains (r .URL .Path , "/apps/sso/assume" ) {
66
+ accountRole , err := getEndpoint (r .URL .Path , "/apps/sso/assume/" )
67
+ if err != nil {
68
+ p .logger .Warn (
69
+ "SSO request failed" ,
70
+ zap .String ("session_id" , rr .Upstream .SessionID ),
71
+ zap .String ("request_id" , rr .ID ),
72
+ zap .String ("error" , "malformed SSO request" ),
73
+ )
74
+ } else {
75
+ assumedRole = true
76
+ arr := strings .SplitN (accountRole , "/" , 2 )
77
+ if len (arr ) != 2 {
78
+ return p .handleHTTPRenderError (ctx , w , r , rr , fmt .Errorf ("Malformed SSO request" ))
79
+ }
80
+ accountID = arr [0 ]
81
+ roleName = arr [1 ]
82
+ }
83
+ }
84
+
57
85
type roleEntry struct {
58
86
Name string
59
87
AccountID string
@@ -73,12 +101,39 @@ func (p *Portal) handleHTTPAppsAwsSso(ctx context.Context, w http.ResponseWriter
73
101
AccountID : arr [1 ],
74
102
}
75
103
roles = append (roles , role )
104
+
105
+ if assumedRole {
106
+ if (role .Name == roleName ) && (role .AccountID == accountID ) {
107
+ authorizedRole = true
108
+ p .logger .Debug (
109
+ "SSO assume role request received" ,
110
+ zap .String ("session_id" , rr .Upstream .SessionID ),
111
+ zap .String ("request_id" , rr .ID ),
112
+ zap .String ("role_name" , roleName ),
113
+ zap .String ("account_id" , accountID ),
114
+ )
115
+ }
116
+ }
117
+ }
118
+
119
+ if assumedRole {
120
+ if ! authorizedRole {
121
+ p .logger .Debug (
122
+ "Unauthorized SSO assume role request" ,
123
+ zap .String ("session_id" , rr .Upstream .SessionID ),
124
+ zap .String ("request_id" , rr .ID ),
125
+ zap .String ("role_name" , roleName ),
126
+ zap .String ("account_id" , accountID ),
127
+ )
128
+ return p .handleHTTPRenderError (ctx , w , r , rr , fmt .Errorf ("Unauthorized SSO assume role request" ))
129
+ }
130
+ p .logger .Debug ("Redirecting to SAML endpoint" )
76
131
}
77
132
78
133
resp .Data ["role_count" ] = len (roles )
79
134
resp .Data ["roles" ] = roles
80
135
81
- content , err := p .ui .Render ("apps_aws_sso " , resp )
136
+ content , err := p .ui .Render ("apps_sso " , resp )
82
137
if err != nil {
83
138
return p .handleHTTPRenderError (ctx , w , r , rr , err )
84
139
}
0 commit comments