cisco/sdk/token.go -rw-r--r-- 1.2 KiB View raw
                                                                                
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
package cisco

import (
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"time"
)

type TokenNew struct {
	Username    string `json:"username"`
	Password    string `json:"password"`
	Description string `json:"description"`
}

type Token struct {
	Key         string    `json:"key"`
	Secret      string    `json:"secret"`
	Description string    `json:"description"`
	Created     time.Time `json:"created"`
}

func NewToken(username string, password string, description string) (*Token, error) {
	var token Token

	hashedPassword := sha256.Sum256([]byte(password))

	data, err := SendRequest(TokenNewRoute, Request{
		Data: &TokenNew{
			Username:    username,
			Password:    hex.EncodeToString(hashedPassword[:]),
			Description: description,
		},
	})

	if err != nil {
		return nil, err
	}

	err = json.Unmarshal(data.([]byte), &token)
	return &token, err
}

func ListToken() ([]Token, error) {
	var ret []Token

	data, err := SendRequest(TokenListRoute, Request{})
	if err != nil {
		return nil, err
	}
	err = json.Unmarshal(data.([]byte), &ret)
	return ret, err
}

func RevokeToken(token string) error {
	_, err := SendRequest(TokenRevokeRoute, Request{
		Arg: token,
	})

	return err
}