Skip to content

Commit

Permalink
Merge pull request #3 from Authing/v3
Browse files Browse the repository at this point in the history
fix: IntrospectAccessTokenOffline and IntrospectToken return type
  • Loading branch information
liaochangjiang authored Oct 26, 2022
2 parents 3f8fad1 + d2c70d6 commit 9c2b6f0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 49 deletions.
23 changes: 16 additions & 7 deletions authentication/authentication_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (client *AuthenticationClient) GetNewAccessTokenByRefreshToken(refreshToken
return string(resp.Body), err
}

func (client *AuthenticationClient) IntrospectToken(token string) (string, error) {
func (client *AuthenticationClient) IntrospectToken(token string) (*dto.IntrospectTokenResult, error) {
url := client.options.AppHost + fmt.Sprintf("/%s/token/introspection", client.options.Protocol)
header := map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
Expand All @@ -356,17 +356,26 @@ func (client *AuthenticationClient) IntrospectToken(token string) (string, error
Headers: client.getReqHeaders(header),
ReqDto: body,
})
return string(resp.Body), err
println(string(resp.Body))
var response dto.IntrospectTokenResult
if err != nil {
return nil, err
}
err = json.Unmarshal(resp.Body, &response)
if err != nil {
return nil, err
}
return &response, err
}

// RevokeToken
// 撤回 Access token 或 Refresh token
func (client *AuthenticationClient) RevokeToken(token string) (string, error) {
func (client *AuthenticationClient) RevokeToken(token string) (bool, error) {
if client.options.Protocol != OIDC && client.options.Protocol != OAUTH {
return constant.StringEmpty, errors.New("初始化 AuthenticationClient 时传入的 protocol 参数必须为 ProtocolEnum.OAUTH 或 ProtocolEnum.OIDC,请检查参数")
return false, errors.New("初始化 AuthenticationClient 时传入的 protocol 参数必须为 ProtocolEnum.OAUTH 或 ProtocolEnum.OIDC,请检查参数")
}
if client.options.AppSecret == "" && client.options.TokenEndPointAuthMethod != constant.None {
return constant.StringEmpty, errors.New("请在初始化 AuthenticationClient 时传入 Secret")
return false, errors.New("请在初始化 AuthenticationClient 时传入 Secret")
}

url := client.options.AppHost + fmt.Sprintf("/%s/token/revocation", client.options.Protocol)
Expand All @@ -390,13 +399,13 @@ func (client *AuthenticationClient) RevokeToken(token string) (string, error) {
default:
body["client_id"] = client.options.AppId
}
resp, err := client.SendProtocolHttpRequest(&ProtocolRequestOption{
_, err := client.SendProtocolHttpRequest(&ProtocolRequestOption{
Url: url,
Method: fasthttp.MethodPost,
Headers: client.getReqHeaders(header),
ReqDto: body,
})
return string(resp.Body), err
return true, err
}

// 拼接登出 URL
Expand Down
54 changes: 24 additions & 30 deletions authentication/authentication_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (

var authenticationClient *AuthenticationClient
var options = AuthenticationClientOptions{
AppId: "635143ae10d1c1b9afb54c39",
AppSecret: "441d87d04bc22a0ac3fc5d7c0736278a",
AppHost: "http://localhost:3000",
RedirectUri: "http://localhost:3003/callback",
AppId: "",
AppSecret: "",
AppHost: "",
RedirectUri: "http://localhost:8989",
InsecureSkipVerify: true,
}

Expand Down Expand Up @@ -65,7 +65,7 @@ func TestAuthUrl(t *testing.T) {
Scope: "offline_access " + constant.DefaultScope,
})
if err != nil {
t.Fatalf("构建授权url失败 %v", err)
panic(err)
return
}
println(result.Url)
Expand Down Expand Up @@ -111,38 +111,32 @@ func TestAuthUrl(t *testing.T) {
}

func TestCode(t *testing.T) {
tokenResponse, err := authenticationClient.GetAccessTokenByCode("g1FZq2O8y3NzHvn3YwtTW7dau6lJD9Icq2ZTUR88d_a")
tokenResponse, err := authenticationClient.GetAccessTokenByCode("BP7D0_o3Ya0TudEP3VolHVVywFDo_e3DFm-19koxQwy")
if err != nil {
t.Fatalf("code校验失败, %v", err)
return
}
fmt.Println(tokenResponse)
}

func TestAccessToken(t *testing.T) {
accessToken := `eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6I` +
`jROVS13OVZIcmVjU1BuT20zNzJubVF4V0ROV1hQbUQxbDdBckNseXhyVTAifQ.eyJ` +
`qdGkiOiJWeFdMRVJPY0FjSy0xR240Y0M3UGciLCJzdWIiOiI2MjkwNzU3ODliNDI0` +
`M2E1MGY2YzA0NTYiLCJpYXQiOjE2NTUyOTgxNzMsImV4cCI6MTY1NjUwNzc3Mywic` +
`2NvcGUiOiJvcGVuaWQgcHJvZmlsZSIsImlzcyI6Imh0dHBzOi8vbG9jYWx0ZXN0Ln` +
`Rlc3QyLmF1dGhpbmctaW5jLmNvL29pZGMiLCJhdWQiOiI2MmE4NTcwYTg1ODU5ZTI` +
`zOTBlZjM4OGYifQ.c64QBODEI_u1KQJaTi_00kz-zquXBwndwvKSRRc2N0LQBX9Ki` +
`mObyLBLEodkdZH61k-JVtI1IFlyupYB1QxejyxpfsbKMCokJ7JaM4J9l1I4Sre9RZ` +
`5CFrP3I03p0eEGiPSfLx3zBswfTz__b9ClnxyAGy3vqj69j3BZxK139ocnG39LHqg` +
`svZ5thY8w4iwFqZE3lZwKNPRdbaRnC5YyP6Y9M8xP9sQNiRTNxNGZPazCsj1RZWhK` +
`VP8a71QyTydSPccIi6s4-GzusO5iKC2bPEGtjwYaWlIK_C-cJtGhXwoYppbUP5sQV` +
`tVUPTVtbua_KYomBjsVIoGaeadV-cg1TA`
charim, err := authenticationClient.IntrospectAccessTokenOffline(accessToken)
if err != nil {
t.Fatalf("access token 校验失败, %v", err)
return
}
fmt.Println(charim)
user, err1 := authenticationClient.GetUserInfo(accessToken)
if err1 != nil {
t.Fatalf("获取用户信息失败,%v", err1)
}
fmt.Println(user)
func TestIntrospectToken(t *testing.T) {
code := "e1I4h2L-9-BaaL87YKtZgjKxsUHppaPW2jsLCsEokuL"
tokenResponse, _ := authenticationClient.GetAccessTokenByCode(code)
resp1, _ := authenticationClient.IntrospectAccessTokenOffline(tokenResponse.AccessToken)
fmt.Printf("%+v\n", resp1.Id)
resp2, _ := authenticationClient.IntrospectAccessTokenOffline(tokenResponse.RefreshToken)
resp3, _ := authenticationClient.IntrospectAccessTokenOffline(tokenResponse.IDToken)
fmt.Printf("%+v\n", resp1)
fmt.Printf("%+v\n", resp2)
fmt.Printf("%+v\n", resp3)
result1, _ := authenticationClient.IntrospectToken(tokenResponse.AccessToken)
result2, _ := authenticationClient.IntrospectToken(tokenResponse.RefreshToken)
result3, _ := authenticationClient.IntrospectToken(tokenResponse.IDToken)
fmt.Printf("%+v\n", result1)
fmt.Printf("%+v\n", result2)
fmt.Printf("%+v\n", result3)
result4, _ := authenticationClient.RevokeToken(tokenResponse.AccessToken)
fmt.Printf("%+v\n", result4)
}

func TestIDToken(t *testing.T) {
Expand Down
22 changes: 10 additions & 12 deletions dto/TokenIntrospectResponse.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package dto


type TokenIntrospectResponse struct{
Active bool `json:"active"`
Sub string `json:"sub,omitempty"`
ClientId string `json:"client_id,omitempty"`
Exp int `json:"exp,omitempty"`
Iat int `json:"iat,omitempty"`
Iss string `json:"iss,omitempty"`
Jti string `json:"jti,omitempty"`
Scope string `json:"scope,omitempty"`
TokenType string `json:"token_type,omitempty"`
type IntrospectTokenResult struct {
Active bool `json:"active"`
Sub string `json:"sub,omitempty"`
ClientId string `json:"client_id,omitempty"`
Exp int `json:"exp,omitempty"`
Iat int `json:"iat,omitempty"`
Iss string `json:"iss,omitempty"`
Jti string `json:"jti,omitempty"`
Scope string `json:"scope,omitempty"`
TokenType string `json:"token_type,omitempty"`
}

0 comments on commit 9c2b6f0

Please sign in to comment.