|
| 1 | +// Copyright 2024 The Casdoor Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package idp |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | + "net/http" |
| 22 | + "time" |
| 23 | + |
| 24 | + "golang.org/x/oauth2" |
| 25 | +) |
| 26 | + |
| 27 | +type KwaiIdProvider struct { |
| 28 | + Client *http.Client |
| 29 | + Config *oauth2.Config |
| 30 | +} |
| 31 | + |
| 32 | +func NewKwaiIdProvider(clientId string, clientSecret string, redirectUrl string) *KwaiIdProvider { |
| 33 | + idp := &KwaiIdProvider{} |
| 34 | + idp.Config = idp.getConfig(clientId, clientSecret, redirectUrl) |
| 35 | + return idp |
| 36 | +} |
| 37 | + |
| 38 | +func (idp *KwaiIdProvider) SetHttpClient(client *http.Client) { |
| 39 | + idp.Client = client |
| 40 | +} |
| 41 | + |
| 42 | +func (idp *KwaiIdProvider) getConfig(clientId string, clientSecret string, redirectUrl string) *oauth2.Config { |
| 43 | + endpoint := oauth2.Endpoint{ |
| 44 | + TokenURL: "https://open.kuaishou.com/oauth2/access_token", |
| 45 | + AuthURL: "https://open.kuaishou.com/oauth2/authorize", // qr code: /oauth2/connect |
| 46 | + } |
| 47 | + |
| 48 | + config := &oauth2.Config{ |
| 49 | + Scopes: []string{"user_info"}, |
| 50 | + Endpoint: endpoint, |
| 51 | + ClientID: clientId, |
| 52 | + ClientSecret: clientSecret, |
| 53 | + RedirectURL: redirectUrl, |
| 54 | + } |
| 55 | + |
| 56 | + return config |
| 57 | +} |
| 58 | + |
| 59 | +type KwaiTokenResp struct { |
| 60 | + Result int `json:"result"` |
| 61 | + ErrorMsg string `json:"error_msg"` |
| 62 | + AccessToken string `json:"access_token"` |
| 63 | + ExpiresIn int `json:"expires_in"` |
| 64 | + RefreshToken string `json:"refresh_token"` |
| 65 | + RefreshTokenExpiresIn int `json:"refresh_token_expires_in"` |
| 66 | + OpenId string `json:"open_id"` |
| 67 | + Scopes []string `json:"scopes"` |
| 68 | +} |
| 69 | + |
| 70 | +// GetToken use code to get access_token |
| 71 | +func (idp *KwaiIdProvider) GetToken(code string) (*oauth2.Token, error) { |
| 72 | + params := map[string]string{ |
| 73 | + "app_id": idp.Config.ClientID, |
| 74 | + "app_secret": idp.Config.ClientSecret, |
| 75 | + "code": code, |
| 76 | + "grant_type": "authorization_code", |
| 77 | + } |
| 78 | + tokenUrl := fmt.Sprintf("%s?app_id=%s&app_secret=%s&code=%s&grant_type=authorization_code", |
| 79 | + idp.Config.Endpoint.TokenURL, params["app_id"], params["app_secret"], params["code"]) |
| 80 | + resp, err := idp.Client.Get(tokenUrl) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + defer resp.Body.Close() |
| 85 | + body, err := io.ReadAll(resp.Body) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + var tokenResp KwaiTokenResp |
| 90 | + err = json.Unmarshal(body, &tokenResp) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + if tokenResp.Result != 1 { |
| 95 | + return nil, fmt.Errorf("get token error: %s", tokenResp.ErrorMsg) |
| 96 | + } |
| 97 | + |
| 98 | + token := &oauth2.Token{ |
| 99 | + AccessToken: tokenResp.AccessToken, |
| 100 | + RefreshToken: tokenResp.RefreshToken, |
| 101 | + Expiry: time.Now().Add(time.Duration(tokenResp.ExpiresIn) * time.Second), |
| 102 | + } |
| 103 | + |
| 104 | + raw := make(map[string]interface{}) |
| 105 | + raw["open_id"] = tokenResp.OpenId |
| 106 | + token = token.WithExtra(raw) |
| 107 | + |
| 108 | + return token, nil |
| 109 | +} |
| 110 | + |
| 111 | +// More details: https://open.kuaishou.com/openapi/user_info |
| 112 | +type KwaiUserInfo struct { |
| 113 | + Result int `json:"result"` |
| 114 | + ErrorMsg string `json:"error_msg"` |
| 115 | + UserInfo struct { |
| 116 | + Head string `json:"head"` |
| 117 | + Name string `json:"name"` |
| 118 | + Sex string `json:"sex"` |
| 119 | + City string `json:"city"` |
| 120 | + } `json:"user_info"` |
| 121 | +} |
| 122 | + |
| 123 | +// GetUserInfo use token to get user profile |
| 124 | +func (idp *KwaiIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error) { |
| 125 | + userInfoUrl := fmt.Sprintf("https://open.kuaishou.com/openapi/user_info?app_id=%s&access_token=%s", |
| 126 | + idp.Config.ClientID, token.AccessToken) |
| 127 | + |
| 128 | + resp, err := idp.Client.Get(userInfoUrl) |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + defer resp.Body.Close() |
| 133 | + |
| 134 | + body, err := io.ReadAll(resp.Body) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + |
| 139 | + var kwaiUserInfo KwaiUserInfo |
| 140 | + err = json.Unmarshal(body, &kwaiUserInfo) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + |
| 145 | + if kwaiUserInfo.Result != 1 { |
| 146 | + return nil, fmt.Errorf("get user info error: %s", kwaiUserInfo.ErrorMsg) |
| 147 | + } |
| 148 | + |
| 149 | + userInfo := &UserInfo{ |
| 150 | + Id: token.Extra("open_id").(string), |
| 151 | + Username: kwaiUserInfo.UserInfo.Name, |
| 152 | + DisplayName: kwaiUserInfo.UserInfo.Name, |
| 153 | + AvatarUrl: kwaiUserInfo.UserInfo.Head, |
| 154 | + Extra: map[string]string{ |
| 155 | + "gender": kwaiUserInfo.UserInfo.Sex, |
| 156 | + "city": kwaiUserInfo.UserInfo.City, |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + return userInfo, nil |
| 161 | +} |
0 commit comments