-
Notifications
You must be signed in to change notification settings - Fork 148
/
login.go
156 lines (137 loc) · 4.1 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2020 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package httpapi
import (
"context"
"encoding/hex"
"fmt"
"net/http"
"time"
"go.uber.org/zap"
"golang.org/x/net/xsrftoken"
"github.com/pipe-cd/pipecd/pkg/jwt"
"github.com/pipe-cd/pipecd/pkg/model"
)
// handleSSOLogin is called when an user requested to login via SSO.
func (h *authHandler) handleSSOLogin(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
// Validate request's payload.
if r.Method != http.MethodPost {
h.handleError(w, r, "Method not allowed", nil)
return
}
projectID := r.FormValue(projectFormKey)
if projectID == "" {
h.handleError(w, r, "Missing project id", nil)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
proj, err := h.projectGetter.Get(ctx, projectID)
if err != nil {
h.handleError(w, r, fmt.Sprintf("Unable to find project %s", projectID), err)
return
}
sso, shared, err := h.findSSOConfig(proj)
if err != nil {
h.handleError(w, r, fmt.Sprintf("Invalid SSO configuration: %v", err), nil)
return
}
if !shared {
if err := sso.Decrypt(h.decrypter); err != nil {
h.handleError(w, r, "Failed to decrypt SSO configuration", err)
return
}
}
var (
stateToken = xsrftoken.Generate(h.stateKey, "", "")
state = hex.EncodeToString([]byte(stateToken))
)
authURL, err := sso.GenerateAuthCodeURL(proj.Id, h.callbackURL, state)
if err != nil {
h.handleError(w, r, "Internal error", err)
return
}
http.SetCookie(w, makeStateCookie(state, h.secureCookie))
http.Redirect(w, r, authURL, http.StatusFound)
}
// handleStaticAdminLogin is called when an user requested to login as a static admin.
func (h *authHandler) handleStaticAdminLogin(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
// Validate request's payload.
if r.Method != http.MethodPost {
h.handleError(w, r, "Method not allowed", nil)
return
}
projectID := r.FormValue(projectFormKey)
if projectID == "" {
h.handleError(w, r, "Missing project id", nil)
return
}
username := r.FormValue(usernameFormKey)
if username == "" {
h.handleError(w, r, "Missing username", nil)
return
}
password := r.FormValue(passwordFormKey)
if password == "" {
h.handleError(w, r, "Missing password", nil)
return
}
var admin *model.ProjectStaticUser
if p, ok := h.projectsInConfig[projectID]; ok {
admin = &model.ProjectStaticUser{
Username: p.StaticAdmin.Username,
PasswordHash: p.StaticAdmin.PasswordHash,
}
} else {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
proj, err := h.projectGetter.Get(ctx, projectID)
if err != nil {
h.handleError(w, r, fmt.Sprintf("Unable to find project: %s", projectID), err)
return
}
if proj.StaticAdminDisabled {
h.handleError(w, r, "Static admin is disabling", nil)
return
}
admin = proj.StaticAdmin
}
if err := admin.Auth(username, password); err != nil {
h.handleError(w, r, "Unable to login", err)
return
}
claims := jwt.NewClaims(
admin.Username,
"",
defaultTokenTTL,
model.Role{
ProjectId: projectID,
ProjectRole: model.Role_ADMIN,
},
)
signedToken, err := h.signer.Sign(claims)
if err != nil {
h.handleError(w, r, "Internal error", err)
return
}
h.logger.Info("a new user has been logged in",
zap.String("user", admin.Username),
zap.String("project-id", projectID),
zap.String("project-role", model.Role_ADMIN.String()),
)
http.SetCookie(w, makeTokenCookie(signedToken, h.secureCookie))
http.Redirect(w, r, rootPath, http.StatusFound)
}