Skip to content

Commit

Permalink
feat: add introspect token
Browse files Browse the repository at this point in the history
  • Loading branch information
waffle-frame committed Mar 13, 2024
1 parent 75a6101 commit a83c872
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
42 changes: 42 additions & 0 deletions casdoorsdk/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"errors"
"fmt"
"strconv"

"github.com/google/uuid"
)

// Token has the same definition as https://github.com/casdoor/casdoor/blob/master/object/token.go#L45
Expand All @@ -42,6 +44,20 @@ type Token struct {
CodeExpireIn int64 `json:"codeExpireIn"`
}

type IntroSpectTokenResult struct {
Active bool `json:"active"`
ClientId string `json:"client_id"`
Username string `json:"username"`
TokenType string `json:"token_type"`
Exp uint `json:"exp"`
Iat uint `json:"iat"`
Nbf uint `json:"nbf"`
Sub uuid.UUID `json:"sub"`
Aud []string `json:"aud"`
Iss string `json:"iss"`
Jti string `json:"jti"`
}

func (c *Client) GetTokens() ([]*Token, error) {
queryMap := map[string]string{
"owner": "admin",
Expand Down Expand Up @@ -121,3 +137,29 @@ func (c *Client) DeleteToken(token *Token) (bool, error) {
_, affected, err := c.modifyToken("delete-token", token, nil)
return affected, err
}

func (c *Client) IntrospectToken(token, tokenTypeHint string) (result *IntroSpectTokenResult, err error) {
queryMap := map[string]string{
"token": token,
"token_type_hint": tokenTypeHint,
}

contentType, body, err := createForm(queryMap)
if err != nil {
return
}

url := c.GetUrl("login/oauth/introspect", nil)

respBytes, err := c.DoPostBytesRaw(url, contentType, body)
if err != nil {
return
}

err = json.Unmarshal(respBytes, &result)
if err != nil {
return
}

return
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/uuid v1.6.0
golang.org/x/oauth2 v0.13.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,8 @@ github.com/google/s2a-go v0.1.3/go.mod h1:Ej+mSEMGRnqRzjc7VtF+jdBwYG5fuJfiZ8ELkj
github.com/google/s2a-go v0.1.4/go.mod h1:Ej+mSEMGRnqRzjc7VtF+jdBwYG5fuJfiZ8ELkjEwM0A=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
github.com/googleapis/enterprise-certificate-proxy v0.1.0/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
github.com/googleapis/enterprise-certificate-proxy v0.2.0/go.mod h1:8C0jb7/mgJe/9KK8Lm7X9ctZC2t60YyIpYEI16jx0Qg=
Expand Down

0 comments on commit a83c872

Please sign in to comment.