forked from apache/pulsar-client-go
/
keyring.go
194 lines (176 loc) · 4.66 KB
/
keyring.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package store
import (
"crypto/sha1"
"encoding/json"
"fmt"
"sync"
"github.com/99designs/keyring"
"github.com/JEFFTheDev/pulsar-client-go/oauth2"
"github.com/JEFFTheDev/pulsar-client-go/oauth2/clock"
)
type KeyringStore struct {
kr keyring.Keyring
clock clock.Clock
lock sync.Mutex
}
// storedItem represents an item stored in the keyring
type storedItem struct {
Audience string
UserName string
Grant oauth2.AuthorizationGrant
}
// NewKeyringStore creates a store based on a keyring.
func NewKeyringStore(kr keyring.Keyring) (*KeyringStore, error) {
return &KeyringStore{
kr: kr,
clock: clock.RealClock{},
}, nil
}
var _ Store = &KeyringStore{}
func (f *KeyringStore) SaveGrant(audience string, grant oauth2.AuthorizationGrant) error {
f.lock.Lock()
defer f.lock.Unlock()
var err error
var userName string
switch grant.Type {
case oauth2.GrantTypeClientCredentials:
if grant.ClientCredentials == nil {
return ErrUnsupportedAuthData
}
userName = grant.ClientCredentials.ClientEmail
case oauth2.GrantTypeDeviceCode:
if grant.Token == nil {
return ErrUnsupportedAuthData
}
userName, err = oauth2.ExtractUserName(*grant.Token)
if err != nil {
return err
}
default:
return ErrUnsupportedAuthData
}
item := storedItem{
Audience: audience,
UserName: userName,
Grant: grant,
}
err = f.setItem(item)
if err != nil {
return err
}
return nil
}
func (f *KeyringStore) LoadGrant(audience string) (*oauth2.AuthorizationGrant, error) {
f.lock.Lock()
defer f.lock.Unlock()
item, err := f.getItem(audience)
if err != nil {
if err == keyring.ErrKeyNotFound {
return nil, ErrNoAuthenticationData
}
return nil, err
}
switch item.Grant.Type {
case oauth2.GrantTypeClientCredentials:
if item.Grant.ClientCredentials == nil {
return nil, ErrUnsupportedAuthData
}
case oauth2.GrantTypeDeviceCode:
if item.Grant.Token == nil {
return nil, ErrUnsupportedAuthData
}
default:
return nil, ErrUnsupportedAuthData
}
return &item.Grant, nil
}
func (f *KeyringStore) WhoAmI(audience string) (string, error) {
f.lock.Lock()
defer f.lock.Unlock()
key := hashKeyringKey(audience)
authItem, err := f.kr.Get(key)
if err != nil {
if err == keyring.ErrKeyNotFound {
return "", ErrNoAuthenticationData
}
return "", fmt.Errorf("unable to get information from the keyring: %v", err)
}
return authItem.Label, nil
}
func (f *KeyringStore) Logout() error {
f.lock.Lock()
defer f.lock.Unlock()
var err error
keys, err := f.kr.Keys()
if err != nil {
return fmt.Errorf("unable to get information from the keyring: %v", err)
}
for _, key := range keys {
err = f.kr.Remove(key)
}
if err != nil {
return fmt.Errorf("unable to update the keyring: %v", err)
}
return nil
}
func (f *KeyringStore) getItem(audience string) (storedItem, error) {
key := hashKeyringKey(audience)
i, err := f.kr.Get(key)
if err != nil {
return storedItem{}, err
}
var grant oauth2.AuthorizationGrant
err = json.Unmarshal(i.Data, &grant)
if err != nil {
// the grant appears to be invalid
return storedItem{}, ErrUnsupportedAuthData
}
return storedItem{
Audience: audience,
UserName: i.Label,
Grant: grant,
}, nil
}
func (f *KeyringStore) setItem(item storedItem) error {
key := hashKeyringKey(item.Audience)
data, err := json.Marshal(item.Grant)
if err != nil {
return err
}
i := keyring.Item{
Key: key,
Data: data,
Label: item.UserName,
Description: "authorization grant",
KeychainNotTrustApplication: false,
KeychainNotSynchronizable: false,
}
err = f.kr.Set(i)
if err != nil {
return fmt.Errorf("unable to update the keyring: %v", err)
}
return nil
}
// hashKeyringKey creates a safe key based on the given string
func hashKeyringKey(s string) string {
h := sha1.New()
h.Write([]byte(s))
bs := h.Sum(nil)
return fmt.Sprintf("%x", bs)
}