Skip to content

Commit

Permalink
feat: add all token APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Dec 1, 2023
1 parent 5bfe01e commit 348c6d9
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 13 deletions.
66 changes: 55 additions & 11 deletions casdoorsdk/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package casdoorsdk
import (
"encoding/json"
"errors"
"fmt"
"strconv"
)

Expand All @@ -41,15 +42,33 @@ type Token struct {
CodeExpireIn int64 `json:"codeExpireIn"`
}

func (c *Client) GetTokens(p int, pageSize int) ([]*Token, int, error) {
func (c *Client) GetTokens() ([]*Token, error) {
queryMap := map[string]string{
"owner": c.OrganizationName,
"p": strconv.Itoa(p),
"pageSize": strconv.Itoa(pageSize),
"owner": c.OrganizationName,
}

url := c.GetUrl("get-tokens", queryMap)

bytes, err := c.DoGetBytes(url)
if err != nil {
return nil, err
}

var tokens []*Token
err = json.Unmarshal(bytes, &tokens)
if err != nil {
return nil, err
}
return tokens, nil
}

func (c *Client) GetPaginationTokens(p int, pageSize int, queryMap map[string]string) ([]*Token, int, error) {
queryMap["owner"] = c.OrganizationName
queryMap["p"] = strconv.Itoa(p)
queryMap["pageSize"] = strconv.Itoa(pageSize)

url := c.GetUrl("get-tokens", queryMap)

response, err := c.DoGetResponse(url)
if err != nil {
return nil, 0, err
Expand All @@ -63,17 +82,42 @@ func (c *Client) GetTokens(p int, pageSize int) ([]*Token, int, error) {
return tokens, int(response.Data2.(float64)), nil
}

func (c *Client) DeleteToken(token *Token) (bool, error) {
token.Owner = "admin"
postBytes, err := json.Marshal(token)
func (c *Client) GetToken(name string) (*Token, error) {
queryMap := map[string]string{
"id": fmt.Sprintf("%s/%s", c.OrganizationName, name),
}

url := c.GetUrl("get-token", queryMap)

bytes, err := c.DoGetBytes(url)
if err != nil {
return false, err
return nil, err
}

resp, err := c.DoPost("delete-token", nil, postBytes, false, false)
var token *Token
err = json.Unmarshal(bytes, &token)
if err != nil {
return false, err
return nil, err
}
return token, nil
}

func (c *Client) UpdateToken(token *Token) (bool, error) {
_, affected, err := c.modifyToken("update-token", token, nil)
return affected, err
}

return resp.Data == "Affected", nil
func (c *Client) UpdateTokenForColumns(token *Token, columns []string) (bool, error) {
_, affected, err := c.modifyToken("update-token", token, columns)
return affected, err
}

func (c *Client) AddToken(token *Token) (bool, error) {
_, affected, err := c.modifyToken("add-token", token, nil)
return affected, err
}

func (c *Client) DeleteToken(token *Token) (bool, error) {
_, affected, err := c.modifyToken("delete-token", token, nil)
return affected, err
}
24 changes: 22 additions & 2 deletions casdoorsdk/token_global.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,28 @@

package casdoorsdk

func GetTokens(p int, pageSize int) ([]*Token, int, error) {
return globalClient.GetTokens(p, pageSize)
func GetTokens() ([]*Token, error) {
return globalClient.GetTokens()
}

func GetPaginationTokens(p int, pageSize int, queryMap map[string]string) ([]*Token, int, error) {
return globalClient.GetPaginationTokens(p, pageSize, queryMap)
}

func GetToken(name string) (*Token, error) {
return globalClient.GetToken(name)
}

func UpdateToken(token *Token) (bool, error) {
return globalClient.UpdateToken(token)
}

func UpdateTokenForColumns(token *Token, columns []string) (bool, error) {
return globalClient.UpdateTokenForColumns(token, columns)
}

func AddToken(token *Token) (bool, error) {
return globalClient.AddToken(token)
}

func DeleteToken(token *Token) (bool, error) {
Expand Down
92 changes: 92 additions & 0 deletions casdoorsdk/token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2023 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 (
"testing"
)

func TestToken(t *testing.T) {
InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication)

name := getRandomName("Token")

// Add a new object
token := &Token{
Owner: "admin",
Name: name,
CreatedTime: GetCurrentTime(),
Code: "abc",
AccessToken: "123456",
}
_, err := AddToken(token)
if err != nil {
t.Fatalf("Failed to add object: %v", err)
}

// Get all objects, check if our added object is inside the list
tokens, err := GetTokens()
if err != nil {
t.Fatalf("Failed to get objects: %v", err)
}
found := false
for _, item := range tokens {
if item.Name == name {
found = true
break
}
}
if !found {
t.Fatalf("Added object not found in list")
}

// Get the object
token, err = GetToken(name)
if err != nil {
t.Fatalf("Failed to get object: %v", err)
}
if token.Name != name {
t.Fatalf("Retrieved object does not match added object: %s != %s", token.Name, name)
}

// Update the object
updatedCode := "Updated Code"
token.Code = updatedCode
_, err = UpdateToken(token)
if err != nil {
t.Fatalf("Failed to update object: %v", err)
}

// Validate the update
updatedToken, err := GetToken(name)
if err != nil {
t.Fatalf("Failed to get updated object: %v", err)
}
if updatedToken.Code != updatedCode {
t.Fatalf("Failed to update object, code mismatch: %s != %s", updatedToken.Code, updatedCode)
}

// Delete the object
_, err = DeleteToken(token)
if err != nil {
t.Fatalf("Failed to delete object: %v", err)
}

// Validate the deletion
deletedToken, err := GetToken(name)
if err != nil || deletedToken != nil {
t.Fatalf("Failed to delete object, it's still retrievable")
}
}
25 changes: 25 additions & 0 deletions casdoorsdk/util_modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,28 @@ func (c *Client) modifyWebhook(action string, webhook *Webhook, columns []string

return resp, resp.Data == "Affected", nil
}

// modifyToken is an encapsulation of cert CUD(Create, Update, Delete) operations.
// possible actions are `add-token`, `update-token`, `delete-token`,
func (c *Client) modifyToken(action string, token *Token, columns []string) (*Response, bool, error) {
queryMap := map[string]string{
"id": fmt.Sprintf("%s/%s", token.Owner, token.Name),
}

if len(columns) != 0 {
queryMap["columns"] = strings.Join(columns, ",")
}

token.Owner = c.OrganizationName
postBytes, err := json.Marshal(token)
if err != nil {
return nil, false, err
}

resp, err := c.DoPost(action, queryMap, postBytes, false, false)
if err != nil {
return nil, false, err
}

return resp, resp.Data == "Affected", nil
}

0 comments on commit 348c6d9

Please sign in to comment.