-
Notifications
You must be signed in to change notification settings - Fork 6
/
login.go
85 lines (73 loc) · 2.67 KB
/
login.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package provider
import (
"fmt"
"net/http"
"github.com/zitadel/logging"
)
func (p *IdentityProvider) callbackHandleFunc(w http.ResponseWriter, r *http.Request) {
response := &Response{
PostTemplate: p.postTemplate,
ErrorFunc: func(err error) {
http.Error(w, fmt.Errorf("failed to send response: %w", err).Error(), http.StatusInternalServerError)
},
Issuer: p.GetEntityID(r.Context()),
}
ctx := r.Context()
if err := r.ParseForm(); err != nil {
logging.Error(err)
http.Error(w, fmt.Errorf("failed to parse form: %w", err).Error(), http.StatusInternalServerError)
return
}
requestID := r.Form.Get("id")
if requestID == "" {
err := fmt.Errorf("no requestID provided")
logging.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
authRequest, err := p.storage.AuthRequestByID(r.Context(), requestID)
if err != nil {
logging.Error(err)
response.sendBackResponse(r, w, response.makeDeniedResponse(fmt.Errorf("failed to get request: %w", err).Error(), p.timeFormat))
return
}
response.RequestID = authRequest.GetAuthRequestID()
response.RelayState = authRequest.GetRelayState()
response.ProtocolBinding = authRequest.GetBindingType()
response.AcsUrl = authRequest.GetAccessConsumerServiceURL()
if !authRequest.Done() {
logging.Error(err)
http.Error(w, fmt.Errorf("failed to get entityID: %w", err).Error(), http.StatusInternalServerError)
return
}
entityID, err := p.storage.GetEntityIDByAppID(r.Context(), authRequest.GetApplicationID())
if err != nil {
logging.Error(err)
http.Error(w, fmt.Errorf("failed to get entityID: %w", err).Error(), http.StatusInternalServerError)
return
}
response.Audience = entityID
attrs := &Attributes{}
if err := p.storage.SetUserinfoWithUserID(ctx, authRequest.GetApplicationID(), attrs, authRequest.GetUserID(), []int{}); err != nil {
logging.Error(err)
http.Error(w, fmt.Errorf("failed to get userinfo: %w", err).Error(), http.StatusInternalServerError)
return
}
samlResponse := response.makeSuccessfulResponse(attrs, p.timeFormat)
switch response.ProtocolBinding {
case PostBinding:
if err := createPostSignature(r.Context(), samlResponse, p); err != nil {
logging.Error(err)
response.sendBackResponse(r, w, response.makeResponderFailResponse(fmt.Errorf("failed to sign response: %w", err).Error(), p.timeFormat))
return
}
case RedirectBinding:
if err := createRedirectSignature(r.Context(), samlResponse, p, response); err != nil {
logging.Error(err)
response.sendBackResponse(r, w, response.makeResponderFailResponse(fmt.Errorf("failed to sign response: %w", err).Error(), p.timeFormat))
return
}
}
response.sendBackResponse(r, w, samlResponse)
return
}