mirror of https://github.com/casbin/casnode.git
fix: replace auth/ with casdoor-go-sdk (#330)
Signed-off-by: sh1luo <690898835@qq.com>
This commit is contained in:
parent
a7b3cb168f
commit
edc287f93b
48
auth/jwt.go
48
auth/jwt.go
|
@ -1,48 +0,0 @@
|
|||
// Copyright 2021 The casbin 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 auth
|
||||
|
||||
import "github.com/dgrijalva/jwt-go"
|
||||
|
||||
type Claims struct {
|
||||
Organization string `json:"organization"`
|
||||
Username string `json:"username"`
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
Avatar string `json:"avatar"`
|
||||
Email string `json:"email"`
|
||||
Phone string `json:"phone"`
|
||||
Affiliation string `json:"affiliation"`
|
||||
Tag string `json:"tag"`
|
||||
Language string `json:"language"`
|
||||
Score int `json:"score"`
|
||||
IsAdmin bool `json:"isAdmin"`
|
||||
AccessToken string `json:"accessToken"`
|
||||
jwt.StandardClaims
|
||||
}
|
||||
|
||||
func ParseJwtToken(token string) (*Claims, error) {
|
||||
tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(authConfig.JwtSecret), nil
|
||||
})
|
||||
|
||||
if tokenClaims != nil {
|
||||
if claims, ok := tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid {
|
||||
return claims, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
// Copyright 2021 The casbin 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 auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
func GetOAuthToken(code string, state string) (*oauth2.Token, error) {
|
||||
config := oauth2.Config{
|
||||
ClientID: authConfig.ClientId,
|
||||
ClientSecret: authConfig.ClientSecret,
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: fmt.Sprintf("%s/api/login/oauth/authorize", authConfig.Endpoint),
|
||||
TokenURL: fmt.Sprintf("%s/api/login/oauth/access_token", authConfig.Endpoint),
|
||||
AuthStyle: oauth2.AuthStyleInParams,
|
||||
},
|
||||
//RedirectURL: redirectUri,
|
||||
Scopes: nil,
|
||||
}
|
||||
|
||||
token, err := config.Exchange(context.Background(), code)
|
||||
if err != nil {
|
||||
return token, err
|
||||
}
|
||||
|
||||
if strings.HasPrefix(token.AccessToken, "error:") {
|
||||
return nil, errors.New(strings.TrimLeft(token.AccessToken, "error: "))
|
||||
}
|
||||
|
||||
return token, err
|
||||
}
|
113
auth/user.go
113
auth/user.go
|
@ -1,113 +0,0 @@
|
|||
// Copyright 2021 The casbin 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 auth
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type AuthConfig struct {
|
||||
Endpoint string
|
||||
ClientId string
|
||||
ClientSecret string
|
||||
JwtSecret string
|
||||
OrganizationName string
|
||||
}
|
||||
|
||||
var authConfig AuthConfig
|
||||
|
||||
type User struct {
|
||||
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
|
||||
Name string `xorm:"varchar(100) notnull pk" json:"name"`
|
||||
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
|
||||
UpdatedTime string `xorm:"varchar(100)" json:"updatedTime"`
|
||||
|
||||
Id string `xorm:"varchar(100)" json:"id"`
|
||||
Type string `xorm:"varchar(100)" json:"type"`
|
||||
Password string `xorm:"varchar(100)" json:"password"`
|
||||
DisplayName string `xorm:"varchar(100)" json:"displayName"`
|
||||
Avatar string `xorm:"varchar(255)" json:"avatar"`
|
||||
Email string `xorm:"varchar(100)" json:"email"`
|
||||
Phone string `xorm:"varchar(100)" json:"phone"`
|
||||
Affiliation string `xorm:"varchar(100)" json:"affiliation"`
|
||||
Tag string `xorm:"varchar(100)" json:"tag"`
|
||||
Language string `xorm:"varchar(100)" json:"language"`
|
||||
Score int `json:"score"`
|
||||
IsAdmin bool `json:"isAdmin"`
|
||||
IsGlobalAdmin bool `json:"isGlobalAdmin"`
|
||||
IsForbidden bool `json:"isForbidden"`
|
||||
Hash string `xorm:"varchar(100)" json:"hash"`
|
||||
PreHash string `xorm:"varchar(100)" json:"preHash"`
|
||||
|
||||
Github string `xorm:"varchar(100)" json:"github"`
|
||||
Google string `xorm:"varchar(100)" json:"google"`
|
||||
QQ string `xorm:"qq varchar(100)" json:"qq"`
|
||||
WeChat string `xorm:"wechat varchar(100)" json:"wechat"`
|
||||
|
||||
Properties map[string]string `json:"properties"`
|
||||
}
|
||||
|
||||
func InitConfig(endpoint string, clientId string, clientSecret string, jwtSecret string, organizationName string) {
|
||||
authConfig = AuthConfig{
|
||||
Endpoint: endpoint,
|
||||
ClientId: clientId,
|
||||
ClientSecret: clientSecret,
|
||||
JwtSecret: jwtSecret,
|
||||
OrganizationName: organizationName,
|
||||
}
|
||||
}
|
||||
|
||||
func GetUsers() ([]*User, error) {
|
||||
url := fmt.Sprintf("%s/api/get-users?owner=%s", authConfig.Endpoint, authConfig.OrganizationName)
|
||||
bytes, err := getBytes(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var users []*User
|
||||
err = json.Unmarshal(bytes, &users)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func GetUser(name string) (*User, error) {
|
||||
url := fmt.Sprintf("%s/api/get-user?id=%s/%s", authConfig.Endpoint, authConfig.OrganizationName, name)
|
||||
bytes, err := getBytes(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var user *User
|
||||
err = json.Unmarshal(bytes, &user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func UpdateUser(user User) (bool, error) {
|
||||
return modifyUser("update-user", user)
|
||||
}
|
||||
|
||||
func AddUser(user User) (bool, error) {
|
||||
return modifyUser("add-user", user)
|
||||
}
|
||||
|
||||
func DeleteUser(user User) (bool, error) {
|
||||
return modifyUser("delete-user", user)
|
||||
}
|
77
auth/util.go
77
auth/util.go
|
@ -1,77 +0,0 @@
|
|||
// Copyright 2021 The casbin 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 auth
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
Status string `json:"status"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
Data2 interface{} `json:"data2"`
|
||||
}
|
||||
|
||||
func getBytes(url string) ([]byte, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
bytes, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return bytes, nil
|
||||
}
|
||||
|
||||
func modifyUser(method string, user User) (bool, error) {
|
||||
user.Owner = authConfig.OrganizationName
|
||||
|
||||
url := fmt.Sprintf("%s/api/%s?id=%s/%s&clientId=%s&clientSecret=%s", authConfig.Endpoint, method, user.Owner, user.Name, authConfig.ClientId, authConfig.ClientSecret)
|
||||
userByte, err := json.Marshal(user)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
resp, err := http.Post(url, "text/plain;charset=UTF-8", bytes.NewReader(userByte))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respByte, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
var response Response
|
||||
err = json.Unmarshal(respByte, &response)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if response.Data == "Affected" {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
|
@ -15,8 +15,8 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/casbin/casnode/auth"
|
||||
"github.com/casbin/casnode/object"
|
||||
"github.com/casdoor/casdoor-go-sdk/auth"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
|
|
|
@ -16,7 +16,7 @@ package controllers
|
|||
|
||||
import (
|
||||
beego "github.com/beego/beego/v2/adapter"
|
||||
"github.com/casbin/casnode/auth"
|
||||
"github.com/casdoor/casdoor-go-sdk/auth"
|
||||
)
|
||||
|
||||
var CasdoorEndpoint = beego.AppConfig.String("casdoorEndpoint")
|
||||
|
|
|
@ -16,8 +16,8 @@ package controllers
|
|||
|
||||
import (
|
||||
beego "github.com/beego/beego/v2/adapter"
|
||||
"github.com/casbin/casnode/auth"
|
||||
"github.com/casbin/casnode/util"
|
||||
"github.com/casdoor/casdoor-go-sdk/auth"
|
||||
|
||||
"github.com/casbin/casnode/object"
|
||||
)
|
||||
|
|
1
go.mod
1
go.mod
|
@ -9,6 +9,7 @@ require (
|
|||
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
|
||||
github.com/beego/beego/v2 v2.0.2-0.20210531141155-ea87fba943cf
|
||||
github.com/casbin/google-groups-crawler v0.1.3
|
||||
github.com/casdoor/casdoor-go-sdk v0.0.1
|
||||
github.com/chromedp/chromedp v0.6.10
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||
github.com/elazarl/go-bindata-assetfs v1.0.1 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -69,6 +69,8 @@ github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737/go.mod h1:PmM6
|
|||
github.com/casbin/casbin v1.7.0/go.mod h1:c67qKN6Oum3UF5Q1+BByfFxkwKvhwW57ITjqwtzR1KE=
|
||||
github.com/casbin/google-groups-crawler v0.1.3 h1:kmbzjLK88dtSTk7ycDvjKH6hwVB0z6dAJGpJvvqRFsg=
|
||||
github.com/casbin/google-groups-crawler v0.1.3/go.mod h1:JHKvWP8blOe/Mbob3R4aaU5RvVIOC83eBcCSlKsbKSI=
|
||||
github.com/casdoor/casdoor-go-sdk v0.0.1 h1:zTmejxbl22rKFH4KOJv64oRjB6eE88Ae8vMiz7x3fGE=
|
||||
github.com/casdoor/casdoor-go-sdk v0.0.1/go.mod h1:PlKduZO7RV1E/Gcpfk5Y1LuzROl5ZELn0PCYPGRF6o8=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
|
||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
|
|
|
@ -5,8 +5,8 @@ import (
|
|||
"strconv"
|
||||
|
||||
beego "github.com/beego/beego/v2/adapter"
|
||||
"github.com/casbin/casnode/auth"
|
||||
"github.com/casbin/casnode/util"
|
||||
"github.com/casdoor/casdoor-go-sdk/auth"
|
||||
)
|
||||
|
||||
var CasdoorOrganization = beego.AppConfig.String("casdoorOrganization")
|
||||
|
|
|
@ -20,11 +20,11 @@ import (
|
|||
"strconv"
|
||||
"time"
|
||||
|
||||
awss3 "github.com/aws/aws-sdk-go/service/s3"
|
||||
beego "github.com/beego/beego/v2/adapter"
|
||||
"github.com/qor/oss"
|
||||
"github.com/qor/oss/aliyun"
|
||||
"github.com/qor/oss/s3"
|
||||
awss3 "github.com/aws/aws-sdk-go/service/s3"
|
||||
)
|
||||
|
||||
var ossURL, basicPath string
|
||||
|
@ -70,10 +70,10 @@ func AliyunInit() {
|
|||
return
|
||||
}
|
||||
storage = aliyun.New(&aliyun.Config{
|
||||
AccessID: accessKeyID,
|
||||
AccessID: accessKeyID,
|
||||
AccessKey: accessKeySecret,
|
||||
Bucket: ossBucket,
|
||||
Endpoint: ossEndPoint,
|
||||
Bucket: ossBucket,
|
||||
Endpoint: ossEndPoint,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -88,12 +88,12 @@ func Awss3Init() {
|
|||
return
|
||||
}
|
||||
storage = s3.New(&s3.Config{
|
||||
AccessID: accessKeyID,
|
||||
AccessID: accessKeyID,
|
||||
AccessKey: accessKeySecret,
|
||||
Region: ossRegion,
|
||||
Bucket: ossBucket,
|
||||
Endpoint: ossEndPoint,
|
||||
ACL: awss3.BucketCannedACLPublicRead,
|
||||
Region: ossRegion,
|
||||
Bucket: ossBucket,
|
||||
Endpoint: ossEndPoint,
|
||||
ACL: awss3.BucketCannedACLPublicRead,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -16,8 +16,8 @@ package sync
|
|||
|
||||
import (
|
||||
beego "github.com/beego/beego/v2/adapter"
|
||||
"github.com/casbin/casnode/auth"
|
||||
"github.com/casbin/casnode/object"
|
||||
"github.com/casdoor/casdoor-go-sdk/auth"
|
||||
_ "github.com/go-sql-driver/mysql" // db = mysql
|
||||
//_ "github.com/lib/pq" // db = postgres
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue