forked from euphoria-io/heim
/
agent.go
135 lines (111 loc) · 2.95 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
package backend
import (
"crypto/rand"
"encoding/json"
"fmt"
"net/http"
"time"
"euphoria.io/heim/proto"
"euphoria.io/heim/proto/security"
"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,
Secure: true,
}
return cookie, nil
}
func assignAgent(ctx scope.Context, s *Server) (*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)
}
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) {
cookie, err := r.Cookie(agentCookieName)
if err != nil {
return assignAgent(ctx, s)
}
encoded := []byte{}
if err := s.sc.Decode(agentCookieName, cookie.Value, &encoded); err != nil {
return assignAgent(ctx, s)
}
ac := agentCredentials{}
if err := json.Unmarshal(encoded, &ac); err != nil {
return assignAgent(ctx, s)
}
agent, err := s.b.AgentTracker().Get(ctx, ac.ID)
if err != nil {
return assignAgent(ctx, s)
}
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
}