forked from euphoria-io/heim
/
agent.go
176 lines (147 loc) · 4.05 KB
/
agent.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package backend
import (
"crypto/rand"
"encoding/json"
"fmt"
"net/http"
"time"
"euphoria.io/heim/proto"
"euphoria.io/heim/proto/security"
"euphoria.io/heim/proto/snowflake"
"euphoria.io/scope"
"github.com/gorilla/securecookie"
)
const (
agentCookieName = "a"
agentCookieDuration = 365 * 24 * time.Hour
)
func newAgentCredentials(agent *proto.Agent, agentKey *security.ManagedKey) (*agentCredentials, error) {
if agentKey.Encrypted() {
return nil, security.ErrKeyMustBeDecrypted
}
ac := &agentCredentials{
ID: agent.IDString(),
Key: agentKey.Plaintext,
}
return ac, nil
}
type agentCredentials struct {
ID string `json:"i"`
Key []byte `json:"k"`
}
func (ac *agentCredentials) Cookie(sc *securecookie.SecureCookie) (*http.Cookie, error) {
encoded, err := json.Marshal(ac)
if err != nil {
return nil, err
}
secured, err := sc.Encode(agentCookieName, encoded)
if err != nil {
return nil, err
}
cookie := &http.Cookie{
Name: agentCookieName,
Value: secured,
Path: "/",
Expires: time.Now().Add(agentCookieDuration),
HttpOnly: true,
}
if !Config.SetInsecureCookies {
cookie.Secure = true
}
return cookie, nil
}
func assignAgent(ctx scope.Context, s *Server, bot bool) (*proto.Agent, *http.Cookie, *security.ManagedKey, error) {
agentKey := &security.ManagedKey{
KeyType: proto.AgentKeyType,
Plaintext: make([]byte, proto.AgentKeyType.KeySize()),
}
_, err := rand.Read(agentKey.Plaintext)
if err != nil {
return nil, nil, nil, fmt.Errorf("agent generation error: %s", err)
}
var agentID []byte
if s.agentIDGenerator != nil {
agentID, err = s.agentIDGenerator()
if err != nil {
return nil, nil, nil, fmt.Errorf("agent generation error: %s", err)
}
}
agent, err := proto.NewAgent(agentID, agentKey)
if err != nil {
return nil, nil, nil, fmt.Errorf("agent generation error: %s", err)
}
agent.Bot = bot
if err := s.b.AgentTracker().Register(ctx, agent); err != nil {
return nil, nil, nil, fmt.Errorf("agent generation error: %s", err)
}
ac, err := newAgentCredentials(agent, agentKey)
if err != nil {
return nil, nil, nil, fmt.Errorf("agent generation error: %s", err)
}
cookie, err := ac.Cookie(s.sc)
if err != nil {
return nil, nil, nil, fmt.Errorf("agent generation error: %s", err)
}
return agent, cookie, agentKey, nil
}
func getAgent(
ctx scope.Context, s *Server, r *http.Request) (
*proto.Agent, *http.Cookie, *security.ManagedKey, error) {
if err := r.ParseForm(); err != nil {
return nil, nil, nil, err
}
bot := r.Form.Get("h") != "1"
cookie, err := r.Cookie(agentCookieName)
if err != nil {
return assignAgent(ctx, s, bot)
}
encoded := []byte{}
if err := s.sc.Decode(agentCookieName, cookie.Value, &encoded); err != nil {
return assignAgent(ctx, s, bot)
}
ac := agentCredentials{}
if err := json.Unmarshal(encoded, &ac); err != nil {
return assignAgent(ctx, s, bot)
}
agent, err := s.b.AgentTracker().Get(ctx, ac.ID)
if err != nil {
return assignAgent(ctx, s, bot)
}
cookie, err = ac.Cookie(s.sc)
if err != nil {
return nil, nil, nil, err
}
agentKey := &security.ManagedKey{
KeyType: proto.AgentKeyType,
Plaintext: ac.Key,
}
return agent, cookie, agentKey, nil
}
func getClient(ctx scope.Context, s *Server, r *http.Request) (*proto.Client, *http.Cookie, *security.ManagedKey, error) {
agent, cookie, agentKey, err := getAgent(ctx, s, r)
if err != nil {
return nil, nil, nil, err
}
client := &proto.Client{
Agent: agent,
Authorization: proto.Authorization{
ClientKey: agentKey,
},
}
client.FromRequest(ctx, r)
// Look up account associated with agent.
var accountID snowflake.Snowflake
if err := accountID.FromString(agent.AccountID); agent.AccountID != "" && err == nil {
if err := client.AuthenticateWithAgent(ctx, s.b, agent, agentKey); err != nil {
fmt.Printf("agent auth failed: %s\n", err)
switch err {
case proto.ErrAccessDenied:
// allow session to proceed, but agent will not be logged into account
agent.AccountID = ""
default:
return nil, nil, nil, err
}
}
}
return client, cookie, agentKey, nil
}