Commit 4bf1af8d authored by Eric's avatar Eric

[update] modify auth sign

parent 503ad9e5
......@@ -15,7 +15,7 @@ import (
)
type ClientAuth interface {
Sign() string
Sign() (date, sign string)
}
type Client struct {
......@@ -97,7 +97,9 @@ func (c *Client) SetAuthHeader(r *http.Request, params ...map[string]string) {
return
}
if c.Auth != nil {
r.Header.Set("Authorization", c.Auth.Sign())
date, sign := c.Auth.Sign()
r.Header.Set("Date", date)
r.Header.Set("Authorization", sign)
}
}
......
......@@ -24,10 +24,10 @@ type AccessKey struct {
Value string
}
func (ak AccessKey) Sign() string {
func (ak AccessKey) Sign() (string, string) {
date := common.HTTPGMTDate()
signature := common.MakeSignature(ak.Secret, date)
return fmt.Sprintf("Sign %s:%s", ak.Id, signature)
return date, fmt.Sprintf("Sign %s:%s", ak.Id, signature)
}
func (ak *AccessKey) LoadAccessKeyFromStr(key string) error {
......
package service
import (
"cocogo/pkg/logger"
"cocogo/pkg/model"
"encoding/json"
"fmt"
"cocogo/pkg/logger"
"cocogo/pkg/model"
)
func GetSystemUserAssetAuthInfo(systemUserID, assetID string) (info model.SystemUserAuthInfo) {
......@@ -14,7 +15,7 @@ func GetSystemUserAssetAuthInfo(systemUserID, assetID string) (info model.System
func GetSystemUserAuthInfo(systemUserID string) (info model.SystemUserAuthInfo) {
Url := fmt.Sprintf(SystemUserAuthInfoURL, systemUserID)
err := client.Get(Url, &info, true)
err := authClient.Get(Url, &info)
if err != nil {
logger.Error("Get system user auth info failed")
}
......
package service
import (
"path"
"path/filepath"
"cocogo/pkg/common"
"cocogo/pkg/config"
"cocogo/pkg/model"
)
type ClientAuth interface {
Sign() string
}
type WrapperClient struct {
Http *common.Client
AuthClient *common.Client
Auth ClientAuth
BaseHost string
}
func (c *WrapperClient) LoadAuth() error {
keyPath := config.Conf.AccessKeyFile
if !path.IsAbs(config.Conf.AccessKeyFile) {
keyPath = filepath.Join(config.Conf.RootPath, keyPath)
}
ak := AccessKey{Value: config.Conf.AccessKey, Path: keyPath}
err := ak.Load()
if err != nil {
return err
}
c.Auth = ak
return nil
}
func (c *WrapperClient) CheckAuth() error {
var user model.User
err := c.Http.Get("UserProfileUrl", &user)
if err != nil {
return err
}
return nil
}
func (c *WrapperClient) Get(url string, res interface{}, needAuth bool) error {
if needAuth {
return c.AuthClient.Get(c.BaseHost+url, res)
} else {
return c.Http.Get(c.BaseHost+url, res)
}
}
func (c *WrapperClient) Post(url string, data interface{}, res interface{}, needAuth bool) error {
if needAuth {
return c.AuthClient.Post(url, data, res)
} else {
return c.Http.Post(url, data, res)
}
}
func (c *WrapperClient) Delete(url string, res interface{}, needAuth bool) error {
if needAuth {
return c.AuthClient.Delete(url, res)
} else {
return c.Http.Delete(url, res)
}
}
func (c *WrapperClient) Put(url string, data interface{}, res interface{}, needAuth bool) error {
if needAuth {
return c.AuthClient.Put(url, data, res)
} else {
return c.Http.Put(url, data, res)
}
}
func (c *WrapperClient) Patch(url string, data interface{}, res interface{}, needAuth bool) error {
if needAuth {
return c.AuthClient.Patch(url, data, res)
} else {
return c.Http.Patch(url, data, res)
}
}
package service
var client = WrapperClient{}
import (
"path"
"path/filepath"
"strings"
"cocogo/pkg/common"
"cocogo/pkg/config"
)
var client = common.NewClient(10)
var authClient = common.NewClient(10)
var baseHost string
func Initial() {
keyPath := config.Conf.AccessKeyFile
baseHost = strings.TrimRight(config.Conf.CoreHost, "/")
if !path.IsAbs(config.Conf.AccessKeyFile) {
keyPath = filepath.Join(config.Conf.RootPath, keyPath)
}
ak := AccessKey{Value: config.Conf.AccessKey, Path: keyPath}
_ = ak.Load()
authClient.Auth = ak
}
......@@ -3,6 +3,7 @@ package service
const (
UserAuthURL = "/api/users/v1/auth/" // post 验证用户登陆
UserProfileURL = "/api/users/v1/profile/" // 获取当前用户的基本信息
UserUserURL = "/api/users/v1/users/%s/" // 获取用户信息
SystemUserAssetAuthURL = "/api/assets/v1/system-user/%s/asset/%s/auth-info/" // 该系统用户对某资产的授权
SystemUserAuthInfoURL = "/api/assets/v1/system-user/%s/auth-info/" // 该系统用户的授权
......
package service
import (
"fmt"
"cocogo/pkg/logger"
"cocogo/pkg/model"
)
func Authenticate(username, password, publicKey, remoteAddr, loginType string) *model.User {
return &model.User{Id: "1111111111", Username: "admin", Name: "广宏伟"}
func Authenticate(username, password, publicKey, remoteAddr, loginType string) (user model.User) {
data := map[string]string{
"username": username,
"password": password,
"public_key": publicKey,
"remote_addr": remoteAddr,
"login_type": loginType}
var resp struct {
Token string `json:"token"`
User model.User `json:"user"`
}
err := client.Post(baseHost+UserAuthURL, data, &resp)
if err != nil {
logger.Error(err)
}
return resp.User
}
func GetUserProfile(userId string) (user model.User) {
Url := fmt.Sprintf(baseHost+UserUserURL, userId)
err := authClient.Get(Url, &user)
if err != nil {
logger.Error(err)
}
return
}
func LoadUserByUsername(user *model.User) {
func CheckUserCookie(sessionId, csrfToken string) (user model.User) {
client.SetCookie("csrftoken", csrfToken)
client.SetCookie("sessionid", sessionId)
err := client.Get(baseHost+UserProfileURL, &user)
if err != nil {
logger.Error(err)
}
return
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment