Skip to content

Commit dda6fc7

Browse files
committed
use secretStorage to store oauth tokens
1 parent a59d4e7 commit dda6fc7

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

cmd/src/login.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/sourcegraph/src-cli/internal/api"
1313
"github.com/sourcegraph/src-cli/internal/cmderrors"
14-
"github.com/sourcegraph/src-cli/internal/keyring"
1514
"github.com/sourcegraph/src-cli/internal/oauthdevice"
1615
)
1716

@@ -126,11 +125,6 @@ func loginCmd(ctx context.Context, p loginParams) error {
126125
noToken := cfg.AccessToken == ""
127126
endpointConflict := endpointArg != cfg.Endpoint
128127

129-
secretStore, err := keyring.Open()
130-
if err != nil {
131-
printProblem(fmt.Sprintf("could not open keyring for secret storage: %s", err))
132-
}
133-
134128
cfg.Endpoint = endpointArg
135129

136130
if p.useDeviceFlow {
@@ -141,7 +135,7 @@ func loginCmd(ctx context.Context, p loginParams) error {
141135
return cmderrors.ExitCode1
142136
}
143137

144-
if err := oauthdevice.StoreToken(secretStore, token); err != nil {
138+
if err := oauthdevice.StoreToken(token); err != nil {
145139
printProblem(fmt.Sprintf("Failed to store token in keyring store: %s", err))
146140
return cmderrors.ExitCode1
147141
}

cmd/src/main.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/sourcegraph/sourcegraph/lib/errors"
1616

1717
"github.com/sourcegraph/src-cli/internal/api"
18-
"github.com/sourcegraph/src-cli/internal/keyring"
1918
"github.com/sourcegraph/src-cli/internal/oauthdevice"
2019
)
2120

@@ -133,13 +132,12 @@ func (c *config) apiClient(flags *api.Flags, out io.Writer) api.Client {
133132
ProxyURL: c.ProxyURL,
134133
ProxyPath: c.ProxyPath,
135134
}
136-
store, err := keyring.Open()
137-
if err != nil {
138-
panic("HALP")
139-
}
140135

141-
if t, err := oauthdevice.LoadToken(store, c.Endpoint); err == nil {
142-
opts.OAuthToken = t
136+
// Only use OAuth if we do not have SRC_ACCESS_TOKEN set
137+
if c.AccessToken == "" {
138+
if t, err := oauthdevice.LoadToken(c.Endpoint); err == nil {
139+
opts.OAuthToken = t
140+
}
143141
}
144142

145143
return api.NewClient(opts)

internal/oauthdevice/device_flow.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"testing"
1414
"time"
1515

16-
"github.com/sourcegraph/src-cli/internal/keyring"
16+
"github.com/sourcegraph/src-cli/internal/secrets"
1717

1818
"github.com/sourcegraph/sourcegraph/lib/errors"
1919
)
@@ -389,7 +389,11 @@ func (t *Token) ExpiringIn(d time.Duration) bool {
389389
return future.After(t.ExpiresAt)
390390
}
391391

392-
func StoreToken(store *keyring.Store, token *Token) error {
392+
func StoreToken(token *Token) error {
393+
store, err := secrets.Store()
394+
if err != nil {
395+
return err
396+
}
393397
data, err := json.Marshal(token)
394398
if err != nil {
395399
return errors.Wrap(err, "failed to marshal token")
@@ -399,18 +403,23 @@ func StoreToken(store *keyring.Store, token *Token) error {
399403
return errors.New("token endpoint cannot be empty when storing the token")
400404
}
401405

402-
key := fmt.Sprintf("%s <%s>", KeyOAuth, token.Endpoint)
403-
return store.Set(key, data)
406+
key := fmt.Sprintf("oauth[%s]", token.Endpoint)
407+
return store.Put(key, data)
404408
}
405409

406-
func LoadToken(store *keyring.Store, endpoint string) (*Token, error) {
407-
key := fmt.Sprintf("%s <%s>", KeyOAuth, endpoint)
408-
var t Token
410+
func LoadToken(endpoint string) (*Token, error) {
411+
store, err := secrets.Store()
412+
if err != nil {
413+
return nil, err
414+
}
415+
416+
key := fmt.Sprintf("oauth[%s]", endpoint)
409417
data, err := store.Get(key)
410418
if err != nil {
411-
return nil, errors.Wrap(err, "failed to get token from store")
419+
return nil, err
412420
}
413421

422+
var t Token
414423
if err := json.Unmarshal(data, &t); err != nil {
415424
return nil, errors.Wrap(err, "failed to unmarshall token")
416425
}

0 commit comments

Comments
 (0)