/
dexlogin.go
73 lines (65 loc) · 2.44 KB
/
dexlogin.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
/*
Copyright (C) 2020 Serge ALEXANDRE
This file is part of koobind project
koobind is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
koobind is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with koobind. If not, see <http://www.gnu.org/licenses/>.
*/
package v1
import (
"encoding/json"
"fmt"
"github.com/koobind/koobind/koomgr/apis/proto"
"github.com/koobind/koobind/koomgr/internal/providers"
"github.com/koobind/koobind/koomgr/internal/servers/handlers"
"net/http"
)
type DexLoginHandler struct {
handlers.BaseHandler
Providers providers.ProviderChain
}
func (this *DexLoginHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
var requestPayload proto.DexLoginRequest
err := json.NewDecoder(request.Body).Decode(&requestPayload)
if err != nil {
this.HttpError(response, err.Error(), http.StatusBadRequest)
return
}
usr, ok, err := this.Providers.Login(requestPayload.Login, requestPayload.Password)
if err != nil {
this.HttpError(response, "Server error. Check server logs", http.StatusInternalServerError)
return
}
if ok {
userToken, err := this.TokenBasket.NewUserToken(usr)
if err != nil {
this.HttpError(response, "Server error. Check server logs", http.StatusInternalServerError)
} else {
data := proto.DexLoginResponse{
Name: userToken.Spec.User.Name,
Uid: userToken.Spec.User.Uid,
EmailVerified: false,
Groups: userToken.Spec.User.Groups,
Token: userToken.Token,
}
if len(userToken.Spec.User.CommonNames) > 0 {
data.CommonName = userToken.Spec.User.CommonNames[0]
}
if len(userToken.Spec.User.Emails) > 0 {
data.Email = userToken.Spec.User.Emails[0]
}
this.Logger.Info(fmt.Sprintf("Login successful for user:'%s' uid:%s, groups=%v with token:'%s'", usr.Name, usr.Uid, usr.Groups, data.Token))
this.ServeJSON(response, data)
}
} else {
this.Logger.Info(fmt.Sprintf("Invalid login for user '%s'.", requestPayload.Login))
this.HttpError(response, "Unauthorized", http.StatusUnauthorized)
}
}