forked from casdoor/casdoor-go-sdk
/
token.go
160 lines (134 loc) · 4.57 KB
/
token.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
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
//
// Licensed 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 casdoorsdk
import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"golang.org/x/oauth2"
)
// Token has the same definition as https://github.com/casdoor/casdoor/blob/master/object/token.go#L45
type Token struct {
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
Name string `xorm:"varchar(100) notnull pk" json:"name"`
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
Application string `xorm:"varchar(100)" json:"application"`
Organization string `xorm:"varchar(100)" json:"organization"`
User string `xorm:"varchar(100)" json:"user"`
Code string `xorm:"varchar(100) index" json:"code"`
AccessToken string `xorm:"mediumtext" json:"accessToken"`
RefreshToken string `xorm:"mediumtext" json:"refreshToken"`
ExpiresIn int `json:"expiresIn"`
Scope string `xorm:"varchar(100)" json:"scope"`
TokenType string `xorm:"varchar(100)" json:"tokenType"`
CodeChallenge string `xorm:"varchar(100)" json:"codeChallenge"`
CodeIsUsed bool `json:"codeIsUsed"`
CodeExpireIn int64 `json:"codeExpireIn"`
}
// GetOAuthToken gets the pivotal and necessary secret to interact with the Casdoor server
func (c *Client) GetOAuthToken(code string, state string) (*oauth2.Token, error) {
config := oauth2.Config{
ClientID: c.ClientId,
ClientSecret: c.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf("%s/api/login/oauth/authorize", c.Endpoint),
TokenURL: fmt.Sprintf("%s/api/login/oauth/access_token", c.Endpoint),
AuthStyle: oauth2.AuthStyleInParams,
},
// RedirectURL: redirectUri,
Scopes: nil,
}
token, err := config.Exchange(context.Background(), code)
if err != nil {
return token, err
}
if strings.HasPrefix(token.AccessToken, "error:") {
return nil, errors.New(strings.TrimLeft(token.AccessToken, "error: "))
}
return token, err
}
func GetOAuthToken(code string, state string) (*oauth2.Token, error) {
return globalClient.GetOAuthToken(code, state)
}
// RefreshOAuthToken refreshes the OAuth token
func (c *Client) RefreshOAuthToken(refreshToken string) (*oauth2.Token, error) {
config := oauth2.Config{
ClientID: c.ClientId,
ClientSecret: c.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf("%s/api/login/oauth/authorize", c.Endpoint),
TokenURL: fmt.Sprintf("%s/api/login/oauth/refresh_token", c.Endpoint),
AuthStyle: oauth2.AuthStyleInParams,
},
// RedirectURL: redirectUri,
Scopes: nil,
}
token, err := config.TokenSource(context.Background(), &oauth2.Token{RefreshToken: refreshToken}).Token()
if err != nil {
return token, err
}
if strings.HasPrefix(token.AccessToken, "error:") {
return nil, errors.New(strings.TrimLeft(token.AccessToken, "error: "))
}
return token, err
}
func RefreshOAuthToken(refreshToken string) (*oauth2.Token, error) {
return globalClient.RefreshOAuthToken(refreshToken)
}
func (c *Client) GetTokens(p int, pageSize int) ([]*Token, int, error) {
queryMap := map[string]string{
"owner": c.OrganizationName,
"p": strconv.Itoa(p),
"pageSize": strconv.Itoa(pageSize),
}
url := c.GetUrl("get-tokens", queryMap)
response, err := c.DoGetResponse(url)
if err != nil {
return nil, 0, err
}
bytes, err := json.Marshal(response.Data)
if err != nil {
return nil, 0, err
}
var tokens []*Token
err = json.Unmarshal(bytes, &tokens)
if err != nil {
return nil, 0, err
}
return tokens, int(response.Data2.(float64)), nil
}
func GetTokens(p int, pageSize int) ([]*Token, int, error) {
return globalClient.GetTokens(p, pageSize)
}
func (c *Client) DeleteToken(name string) (bool, error) {
organization := Organization{
Owner: "admin",
Name: name,
}
postBytes, err := json.Marshal(organization)
if err != nil {
return false, err
}
resp, err := c.DoPost("delete-token", nil, postBytes, false, false)
if err != nil {
return false, err
}
return resp.Data == "Affected", nil
}
func DeleteToken(name string) (bool, error) {
return globalClient.DeleteToken(name)
}