forked from casbin/bee
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add aliyun captcha (beego#833)
* feat: add aliyun captcha provider * Rename App key * fix typo * Rename HMACSHA1 & Reused clientId2 and clientSecret2 * Update ProviderEditPage.js * Delete unused import Co-authored-by: Gucheng <[email protected]>
- Loading branch information
Showing
9 changed files
with
272 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright 2022 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 captcha | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/casdoor/casdoor/util" | ||
) | ||
|
||
const AliyunCaptchaVerifyUrl = "http://afs.aliyuncs.com" | ||
|
||
type AliyunCaptchaProvider struct { | ||
} | ||
|
||
func NewAliyunCaptchaProvider() *AliyunCaptchaProvider { | ||
captcha := &AliyunCaptchaProvider{} | ||
return captcha | ||
} | ||
|
||
func contentEscape(str string) string { | ||
str = strings.Replace(str, " ", "%20", -1) | ||
str = url.QueryEscape(str) | ||
return str | ||
} | ||
|
||
func (captcha *AliyunCaptchaProvider) VerifyCaptcha(token, clientSecret string) (bool, error) { | ||
pathData, err := url.ParseQuery(token) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
pathData["Action"] = []string{"AuthenticateSig"} | ||
pathData["Format"] = []string{"json"} | ||
pathData["SignatureMethod"] = []string{"HMAC-SHA1"} | ||
pathData["SignatureNonce"] = []string{strconv.FormatInt(time.Now().UnixNano(), 10)} | ||
pathData["SignatureVersion"] = []string{"1.0"} | ||
pathData["Timestamp"] = []string{time.Now().UTC().Format("2006-01-02T15:04:05Z")} | ||
pathData["Version"] = []string{"2018-01-12"} | ||
|
||
var keys []string | ||
for k := range pathData { | ||
keys = append(keys, k) | ||
} | ||
sort.Strings(keys) | ||
|
||
sortQuery := "" | ||
for _, k := range keys { | ||
sortQuery += k + "=" + contentEscape(pathData[k][0]) + "&" | ||
} | ||
sortQuery = strings.TrimSuffix(sortQuery, "&") | ||
|
||
stringToSign := fmt.Sprintf("GET&%s&%s", url.QueryEscape("/"), url.QueryEscape(sortQuery)) | ||
|
||
signature := util.GetHmacSha1(clientSecret+"&", stringToSign) | ||
|
||
resp, err := http.Get(fmt.Sprintf("%s?%s&Signature=%s", AliyunCaptchaVerifyUrl, sortQuery, url.QueryEscape(signature))) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
type captchaResponse struct { | ||
Code int `json:"Code"` | ||
Msg string `json:"Msg"` | ||
} | ||
captchaResp := &captchaResponse{} | ||
|
||
err = json.Unmarshal(body, captchaResp) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if captchaResp.Code != 100 { | ||
return false, errors.New(captchaResp.Msg) | ||
} | ||
|
||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2022 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 util | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha1" | ||
"encoding/base64" | ||
) | ||
|
||
func GetHmacSha1(keyStr, value string) string { | ||
key := []byte(keyStr) | ||
mac := hmac.New(sha1.New, key) | ||
mac.Write([]byte(value)) | ||
res := base64.StdEncoding.EncodeToString(mac.Sum(nil)) | ||
|
||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.