forked from openshift-online/ocm-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secure_store.go
56 lines (45 loc) · 1.33 KB
/
secure_store.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
package main
import (
"fmt"
"os"
"github.com/openshift-online/ocm-sdk-go/authentication"
"github.com/openshift-online/ocm-sdk-go/authentication/securestore"
)
func main() {
// Modify the following variable to match your OS:
keyring := "keychain"
// Create a context:
clientId := "ocm-cli"
// Create the connection, and remember to close it:
token, err := authentication.InitiateAuthCode(clientId)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't get token: %v\n", err)
os.Exit(1)
}
// Get a list of available backends on the current OS
available := securestore.AvailableBackends()
fmt.Printf("Available backends: %v\n", available)
// Create bytes
config := []byte("mybytestringagain")
// Upsert to keyring
err = securestore.UpsertConfigToKeyring(keyring, config)
if err != nil {
fmt.Fprintf(os.Stderr, "Error upserting to keyring: %v", err)
os.Exit(1)
}
// Upsert again to keyring
config = []byte(token)
err = securestore.UpsertConfigToKeyring(keyring, config)
if err != nil {
fmt.Fprintf(os.Stderr, "Error upserting to keyring: %v", err)
os.Exit(1)
}
// Read bytes back from Keyring
readVal, err := securestore.GetConfigFromKeyring(keyring)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading from keyring: %v", err)
os.Exit(1)
}
// Should be a token
fmt.Printf("Read from keyring: %s\n", string(readVal))
}