-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathuser_service.go
84 lines (73 loc) · 2.32 KB
/
user_service.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
/*
Used to consume external authorization.
Given a key parameter, return a user's ability to download the project refs.
Uses https://github.com/bndr/gopencils
*/
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
)
// declare the type of UserAccessGetter
// The url must conform to consumers_spec user_service spec
//type UserAccessGetter func(url string) string
type UserAccessResponse struct {
Access bool `json:"access"`
Status string `json:"status"`
Message string `json:"message"`
RawResponse []byte
Filled bool
}
type UserServiceAuth struct {
Username string
Password string
}
type UserService struct {
Downloader *Downloader
Username, Project, Action string
UserAccessResponse *UserAccessResponse
}
var AllowedActions = []string{"download", "push", "force_push", "admin"}
func (us *UserService) vetAction() bool {
for _, b := range AllowedActions {
if b == us.Action {
return true
}
}
return false
}
func NewUserService(base string, username string, project string, action string) *UserService {
url := fmt.Sprintf("%s?username=%s&project=%s&action=%s", base, username, project, action)
us := &UserService{Downloader: NewDownloader(url), Username: username, Project: project, Action: action}
// This is only here for testing until i figure a better way
// TODO: Find a way to stub this without the ghetto "Filled" hack
us.UserAccessResponse = &UserAccessResponse{Filled: false}
if us.vetAction() != true {
logger.Log(kv{"fn": "NewUserService", "action": fmt.Sprintf("%s is not in AllowedActions", action)})
us.UserAccessResponse.Message = fmt.Sprintf("%s is not in AllowedActions", us.Action)
}
return us
}
// fills UserAccessResponse.RawResponse and pushes json into struct
func (us *UserService) GetResponse() error {
us.Downloader.GetPage()
buf := bytes.NewBuffer(us.Downloader.Response)
us.UserAccessResponse.RawResponse = buf.Bytes()
rdr := bytes.NewReader(us.Downloader.Response)
// read the json into our struct
if err := json.NewDecoder(rdr).Decode(&us.UserAccessResponse); err == nil {
us.UserAccessResponse.Filled = true
return nil
} else {
errors.New(fmt.Sprintf("STATUS ERR: %s", err.Error()))
return err
}
}
func (us *UserService) Can() bool {
if !us.UserAccessResponse.Filled {
us.GetResponse()
}
return us.UserAccessResponse.Access
}