-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
128 lines (115 loc) · 3.13 KB
/
auth.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
package types
import (
"encoding/json"
"io"
"net/http"
"github.com/labstack/echo/v4"
"github.com/scritch007/ShareMinatorApiGenerator/api"
)
type SubAuthentication interface {
Name() string
AddRoutes(r *echo.Echo) error
}
type Authentication struct {
Auths []SubAuthentication
Config *Configuration
}
func (auth *Authentication) GetAuthenticatedUser(w http.ResponseWriter, r *http.Request) (user *Account, err error) {
//TODO KeyWord should be changed
authHeader := r.Header.Get("Authentication")
user = nil
err = nil
if len(authHeader) == 0 {
authHeader = r.URL.Query().Get("Authentication")
if len(authHeader) == 0 {
return nil, nil
}
}
session, err := auth.Config.Db.GetSession(authHeader)
if nil != err {
return nil, err
}
userAccount, err := auth.Config.Db.GetUserAccount(session.UserId)
if nil != err {
return nil, err
}
return userAccount, err
}
func (auth *Authentication) LogOut(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Authentication")
if len(authHeader) == 0 {
authHeader = r.URL.Query().Get("Authentication")
if len(authHeader) == 0 {
http.Error(w, "You're not logged in", http.StatusBadRequest)
return
}
return
}
err := auth.Config.Db.RemoveSession(authHeader)
if nil != err {
http.Error(w, "You're not logged in", http.StatusBadRequest)
}
}
func (auth *Authentication) GetAvailableAuthentications() []string {
res := make([]string, len(auth.Auths))
for i, elem := range auth.Auths {
res[i] = elem.Name()
}
return res
}
func (auth *Authentication) ListUsers(w http.ResponseWriter, r *http.Request) {
user, err := auth.GetAuthenticatedUser(w, r)
if nil == user {
var message string
if nil != err {
message = err.Error()
} else {
message = "You're not allowed to do this"
}
http.Error(w, message, http.StatusUnauthorized)
return
}
search := r.URL.Query().Get("search")
searchParameters := make(map[string]string)
if len(search) != 0 {
searchParameters["login"] = search
searchParameters["id"] = search
}
accounts, _ := auth.Config.Db.ListAccounts(searchParameters)
resp := make([]api.Account, len(accounts))
for i, account := range accounts {
AccountBackend2Api(account, &resp[i])
}
//var tempResult []*Account
b, _ := json.Marshal(resp)
io.WriteString(w, string(b))
}
func (auth *Authentication) GetInfo(w http.ResponseWriter, r *http.Request) {
key := r.URL.Query().Get("key")
shareInfo := api.RequestGetInfoOutput{}
if len(key) == 0 {
shareInfo.ShareLink = false
b, _ := json.Marshal(shareInfo)
io.WriteString(w, string(b))
return
}
result, err := auth.Config.Db.GetShareLink(key)
if err != nil {
shareInfo.ShareLink = false
b, _ := json.Marshal(shareInfo)
io.WriteString(w, string(b))
return
}
shareInfo.ShareLink = true
if result.ShareLink.Type == api.EnumShareByKeyAndPassword {
shareInfo.PasswordProtected = true
} else {
shareInfo.PasswordProtected = false
}
shareInfo.NbDownloads = result.ShareLink.NbDownloads
shareInfo.Type = result.ShareLink.Type
shareInfo.Access = *result.ShareLink.Access
shareInfo.ShareAccess = *result.ShareLink.Access
b, _ := json.Marshal(shareInfo)
io.WriteString(w, string(b))
}