| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"html/template" |
| 7 |
"net/http" |
| 8 |
|
| 9 |
"github.com/go-chi/chi/v5" |
| 10 |
|
| 11 |
"github.com/zitadel/oidc/v3/pkg/op" |
| 12 |
) |
| 13 |
|
| 14 |
const ( |
| 15 |
queryAuthRequestID = "authRequestID" |
| 16 |
) |
| 17 |
|
| 18 |
var ( |
| 19 |
loginTmpl, _ = template.New("login").Parse(` |
| 20 |
<!DOCTYPE html> |
| 21 |
<html> |
| 22 |
<head> |
| 23 |
<meta charset="UTF-8"> |
| 24 |
<title>Login</title> |
| 25 |
</head> |
| 26 |
<body style="display: flex; align-items: center; justify-content: center; height: 100vh;"> |
| 27 |
<form method="POST" action="/login/username" style="height: 200px; width: 200px;"> |
| 28 |
<input type="hidden" name="id" value="{{.ID}}"> |
| 29 |
<div> |
| 30 |
<label for="username">Username:</label> |
| 31 |
<input id="username" name="username" style="width: 100%"> |
| 32 |
</div> |
| 33 |
<div> |
| 34 |
<label for="password">Password:</label> |
| 35 |
<input id="password" name="password" style="width: 100%"> |
| 36 |
</div> |
| 37 |
<p style="color:red; min-height: 1rem;">{{.Error}}</p> |
| 38 |
<button type="submit">Login</button> |
| 39 |
</form> |
| 40 |
</body> |
| 41 |
</html>`) |
| 42 |
) |
| 43 |
|
| 44 |
type login struct { |
| 45 |
authenticate authenticate |
| 46 |
router chi.Router |
| 47 |
callback func(context.Context, string) string |
| 48 |
} |
| 49 |
|
| 50 |
func NewLogin(authenticate authenticate, callback func(context.Context, string) string, issuerInterceptor *op.IssuerInterceptor) *login { |
| 51 |
l := &login{ |
| 52 |
authenticate: authenticate, |
| 53 |
callback: callback, |
| 54 |
} |
| 55 |
l.createRouter(issuerInterceptor) |
| 56 |
return l |
| 57 |
} |
| 58 |
|
| 59 |
func (l *login) createRouter(issuerInterceptor *op.IssuerInterceptor) { |
| 60 |
l.router = chi.NewRouter() |
| 61 |
l.router.Get("/username", l.loginHandler) |
| 62 |
l.router.With(issuerInterceptor.Handler).Post("/username", l.checkLoginHandler) |
| 63 |
} |
| 64 |
|
| 65 |
type authenticate interface { |
| 66 |
CheckUsernamePassword(ctx context.Context, username, password, id string) error |
| 67 |
} |
| 68 |
|
| 69 |
func (l *login) loginHandler(w http.ResponseWriter, r *http.Request) { |
| 70 |
err := r.ParseForm() |
| 71 |
if err != nil { |
| 72 |
http.Error(w, fmt.Sprintf("cannot parse form:%s", err), http.StatusInternalServerError) |
| 73 |
return |
| 74 |
} |
| 75 |
//the oidc package will pass the id of the auth request as query parameter |
| 76 |
//we will use this id through the login process and therefore pass it to the login page |
| 77 |
renderLogin(w, r.FormValue(queryAuthRequestID), nil) |
| 78 |
} |
| 79 |
|
| 80 |
func renderLogin(w http.ResponseWriter, id string, err error) { |
| 81 |
var errMsg string |
| 82 |
if err != nil { |
| 83 |
errMsg = err.Error() |
| 84 |
} |
| 85 |
data := &struct { |
| 86 |
ID string |
| 87 |
Error string |
| 88 |
}{ |
| 89 |
ID: id, |
| 90 |
Error: errMsg, |
| 91 |
} |
| 92 |
err = loginTmpl.Execute(w, data) |
| 93 |
if err != nil { |
| 94 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
func (l *login) checkLoginHandler(w http.ResponseWriter, r *http.Request) { |
| 99 |
err := r.ParseForm() |
| 100 |
if err != nil { |
| 101 |
http.Error(w, fmt.Sprintf("cannot parse form:%s", err), http.StatusInternalServerError) |
| 102 |
return |
| 103 |
} |
| 104 |
username := r.FormValue("username") |
| 105 |
password := r.FormValue("password") |
| 106 |
id := r.FormValue("id") |
| 107 |
err = l.authenticate.CheckUsernamePassword(r.Context(), username, password, id) |
| 108 |
if err != nil { |
| 109 |
renderLogin(w, id, err) |
| 110 |
return |
| 111 |
} |
| 112 |
http.Redirect(w, r, l.callback(r.Context(), id), http.StatusFound) |
| 113 |
} |