-
Notifications
You must be signed in to change notification settings - Fork 32
/
self.go
61 lines (51 loc) · 1.38 KB
/
self.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
package registry
import (
"context"
"errors"
"github.com/manifoldco/torus-cli/apitypes"
"github.com/manifoldco/torus-cli/envelope"
"github.com/manifoldco/torus-cli/primitive"
)
var errUnknownSessionType = errors.New("Unknown session type")
// SelfClient represents the registry `/self` endpoints.
type SelfClient struct {
client RequestDoer
}
type rawSelf struct {
*apitypes.Self
// Shadow over the pure self value
Identity *envelope.Unsigned `json:"identity"`
Auth *envelope.Unsigned `json:"auth"`
}
// Get returns the current identities associated with this token
func (s *SelfClient) Get(ctx context.Context, token string) (*apitypes.Self, error) {
raw := &rawSelf{}
err := tokenRoundTrip(ctx, s.client, token, "GET", "/self", nil, nil, raw)
if err != nil {
return nil, err
}
self := raw.Self
switch raw.Type {
case apitypes.UserSession:
user, err := envelope.ConvertUser(raw.Identity)
if err != nil {
return nil, err
}
self.Identity = user
self.Auth = user
case apitypes.MachineSession:
self.Identity = &envelope.Machine{
ID: raw.Identity.ID,
Version: raw.Identity.Version,
Body: raw.Identity.Body.(*primitive.Machine),
}
self.Auth = &envelope.MachineToken{
ID: raw.Auth.ID,
Version: raw.Auth.Version,
Body: raw.Auth.Body.(*primitive.MachineToken),
}
default:
return nil, errUnknownSessionType
}
return self, nil
}