forked from qvest-digital/loginsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login_form.go
200 lines (177 loc) · 5.47 KB
/
login_form.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package login
import (
"bytes"
"html/template"
"io/ioutil"
"net/http"
"strings"
"github.com/tarent/loginsrv/logging"
"github.com/tarent/loginsrv/model"
)
const partials = `
{{define "styles"}}
<link uic-remove rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link uic-remove rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-social/5.1.1/bootstrap-social.min.css">
<link uic-remove rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<style>
.vertical-offset-100{
padding-top:100px;
}
.login-or-container {
text-align: center;
margin: 0;
margin-bottom: 10px;
clear: both;
color: #6a737c;
font-variant: small-caps;
}
.login-or-hr {
margin-bottom: 0;
position: relative;
top: 28px;
height: 0;
border: 0;
border-top: 1px solid #e4e6e8;
}
.login-or {
display: inline-block;
position: relative;
padding: 10px;
background-color: #FFF;
}
.login-picture {
height: 120px;
border-radius: 3px;
margin-bottom: 10px;
}
</style>
{{end}}
{{define "userInfo"}}
{{with .UserInfo}}
<h1>Welcome {{.Sub}}!</h1>
<br/>
{{if .Picture}}<img class="login-picture" src="{{.Picture}}?s=120">{{end}}
{{if .Name}}<h3>{{.Name}}</h3>{{end}}
{{end}}
<br/>
<a class="btn btn-md btn-primary" href="{{ .Config.LoginPath }}?logout=true">Logout</a>
{{end}}
{{define "login"}}
{{ range $providerName, $opts := .Config.Oauth }}
<a class="btn btn-block btn-lg btn-social btn-{{ $providerName }}" href="{{ $.Config.LoginPath }}/{{ $providerName }}">
<span class="fa fa-{{ $providerName }}"></span> Sign in with {{ $providerName | ucfirst }}
</a>
{{end}}
{{if and (not (eq (len .Config.Backends) 0)) (not (eq (len .Config.Oauth) 0))}}
<div class="login-or-container">
<hr class="login-or-hr">
<div class="login-or lead">or</div>
</div>
{{end}}
{{if not (eq (len .Config.Backends) 0) }}
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title">
<h4>Sign in</h4>
{{ if .Failure}}<div class="alert alert-warning" role="alert">Invalid credentials</div>{{end}}
</div>
</div>
<div class="panel-body">
<form accept-charset="UTF-8" role="form" method="POST" action="{{.Config.LoginPath}}">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Username" name="username" value="{{.UserInfo.Sub}}" type="text">
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" value="">
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="Login">
</fieldset>
</form>
</div>
</div>
{{end}}
{{end}}`
var layout = `<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
{{ template "styles" . }}
</head>
<body>
<uic-fragment name="content">
<div class="container">
<div class="row vertical-offset-100">
<div class="col-md-4 col-md-offset-4">
{{ if .Error}}
<div class="alert alert-danger" role="alert">
<strong>Internal Error. </strong> Please try again later.
</div>
{{end}}
{{if .Authenticated}}
{{template "userInfo" . }}
{{else}}
{{template "login" . }}
{{end}}
</div>
</div>
</div>
</uic-fragment>
</body>
</html>`
type loginFormData struct {
Error bool
Failure bool
Config *Config
Authenticated bool
UserInfo model.UserInfo
}
func writeLoginForm(w http.ResponseWriter, params loginFormData) {
funcMap := template.FuncMap{
"ucfirst": ucfirst,
}
templateName := "loginForm"
if params.Config != nil && params.Config.Template != "" {
templateName = params.Config.Template
}
t := template.New(templateName).Funcs(funcMap)
t = template.Must(t.Parse(partials))
if params.Config != nil && params.Config.Template != "" {
customTemplate, err := ioutil.ReadFile(params.Config.Template)
if err != nil {
logging.Logger.WithError(err).Error()
w.WriteHeader(500)
w.Write([]byte(`Internal Server Error`))
return
}
t, err = t.Parse(string(customTemplate))
if err != nil {
logging.Logger.WithError(err).Error()
w.WriteHeader(500)
w.Write([]byte(`Internal Server Error`))
return
}
} else {
t = template.Must(t.Parse(layout))
}
b := bytes.NewBuffer(nil)
err := t.Execute(b, params)
if err != nil {
logging.Logger.WithError(err).Error()
w.WriteHeader(500)
w.Write([]byte(`Internal Server Error`))
return
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", contentTypeHTML)
if params.Error {
w.WriteHeader(500)
}
w.Write(b.Bytes())
}
func ucfirst(in string) string {
if in == "" {
return ""
}
return strings.ToUpper(in[0:1]) + in[1:]
}