-
Notifications
You must be signed in to change notification settings - Fork 28
/
dummyauth.go
57 lines (48 loc) · 1.46 KB
/
dummyauth.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package auth
import (
"errors"
"net/http"
)
// dummyAuth is a dummy implementation that implements the authentication interface
// but with a dummy user.
// This auth method should only be used for local development
type dummyAuth struct {
loggedIn bool
}
// NewDummyAuth returns a dummy implementation that implements the authentication interface
// but with a dummy user.
// This auth method should only be used for local development
func NewDummyAuth() Provider {
return &dummyAuth{}
}
func (a *dummyAuth) Protect(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !a.loggedIn {
http.Redirect(w, r, "/404", http.StatusTemporaryRedirect)
}
handler(w, r)
}
}
func (a *dummyAuth) GetAuthContext(r *http.Request) (AuthContext, error) {
if a.loggedIn {
return AuthContext{
User: "demo",
}, nil
}
return AuthContext{}, errors.New("user not logged in")
}
func (a *dummyAuth) DisplayLogin() bool {
return true
}
func (_ *dummyAuth) Redirect(w http.ResponseWriter, r *http.Request) {}
func (a *dummyAuth) Login(w http.ResponseWriter, r *http.Request) {
a.loggedIn = true
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
func (a *dummyAuth) Logout(w http.ResponseWriter, r *http.Request) {
a.loggedIn = false
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}