Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
K
koko
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
koko
Commits
4bf1af8d
Commit
4bf1af8d
authored
Apr 30, 2019
by
Eric
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[update] modify auth sign
parent
503ad9e5
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
70 additions
and
100 deletions
+70
-100
client.go
pkg/common/client.go
+4
-2
accesskey.go
pkg/service/accesskey.go
+2
-2
assets.go
pkg/service/assets.go
+4
-3
client.go
pkg/service/client.go
+0
-86
init.go
pkg/service/init.go
+24
-1
urls.go
pkg/service/urls.go
+3
-2
users.go
pkg/service/users.go
+33
-4
No files found.
pkg/common/client.go
View file @
4bf1af8d
...
@@ -15,7 +15,7 @@ import (
...
@@ -15,7 +15,7 @@ import (
)
)
type
ClientAuth
interface
{
type
ClientAuth
interface
{
Sign
()
string
Sign
()
(
date
,
sign
string
)
}
}
type
Client
struct
{
type
Client
struct
{
...
@@ -97,7 +97,9 @@ func (c *Client) SetAuthHeader(r *http.Request, params ...map[string]string) {
...
@@ -97,7 +97,9 @@ func (c *Client) SetAuthHeader(r *http.Request, params ...map[string]string) {
return
return
}
}
if
c
.
Auth
!=
nil
{
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
)
}
}
}
}
...
...
pkg/service/accesskey.go
View file @
4bf1af8d
...
@@ -24,10 +24,10 @@ type AccessKey struct {
...
@@ -24,10 +24,10 @@ type AccessKey struct {
Value
string
Value
string
}
}
func
(
ak
AccessKey
)
Sign
()
string
{
func
(
ak
AccessKey
)
Sign
()
(
string
,
string
)
{
date
:=
common
.
HTTPGMTDate
()
date
:=
common
.
HTTPGMTDate
()
signature
:=
common
.
MakeSignature
(
ak
.
Secret
,
date
)
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
{
func
(
ak
*
AccessKey
)
LoadAccessKeyFromStr
(
key
string
)
error
{
...
...
pkg/service/assets.go
View file @
4bf1af8d
package
service
package
service
import
(
import
(
"cocogo/pkg/logger"
"cocogo/pkg/model"
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"cocogo/pkg/logger"
"cocogo/pkg/model"
)
)
func
GetSystemUserAssetAuthInfo
(
systemUserID
,
assetID
string
)
(
info
model
.
SystemUserAuthInfo
)
{
func
GetSystemUserAssetAuthInfo
(
systemUserID
,
assetID
string
)
(
info
model
.
SystemUserAuthInfo
)
{
...
@@ -14,7 +15,7 @@ func GetSystemUserAssetAuthInfo(systemUserID, assetID string) (info model.System
...
@@ -14,7 +15,7 @@ func GetSystemUserAssetAuthInfo(systemUserID, assetID string) (info model.System
func
GetSystemUserAuthInfo
(
systemUserID
string
)
(
info
model
.
SystemUserAuthInfo
)
{
func
GetSystemUserAuthInfo
(
systemUserID
string
)
(
info
model
.
SystemUserAuthInfo
)
{
Url
:=
fmt
.
Sprintf
(
SystemUserAuthInfoURL
,
systemUserID
)
Url
:=
fmt
.
Sprintf
(
SystemUserAuthInfoURL
,
systemUserID
)
err
:=
client
.
Get
(
Url
,
&
info
,
true
)
err
:=
authClient
.
Get
(
Url
,
&
info
)
if
err
!=
nil
{
if
err
!=
nil
{
logger
.
Error
(
"Get system user auth info failed"
)
logger
.
Error
(
"Get system user auth info failed"
)
}
}
...
...
pkg/service/client.go
deleted
100644 → 0
View file @
503ad9e5
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
)
}
}
pkg/service/init.go
View file @
4bf1af8d
package
service
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
}
pkg/service/urls.go
View file @
4bf1af8d
package
service
package
service
const
(
const
(
UserAuthURL
=
"/api/users/v1/auth/"
// post 验证用户登陆
UserAuthURL
=
"/api/users/v1/auth/"
// post 验证用户登陆
UserProfileURL
=
"/api/users/v1/profile/"
// 获取当前用户的基本信息
UserProfileURL
=
"/api/users/v1/profile/"
// 获取当前用户的基本信息
UserUserURL
=
"/api/users/v1/users/%s/"
// 获取用户信息
SystemUserAssetAuthURL
=
"/api/assets/v1/system-user/%s/asset/%s/auth-info/"
// 该系统用户对某资产的授权
SystemUserAssetAuthURL
=
"/api/assets/v1/system-user/%s/asset/%s/auth-info/"
// 该系统用户对某资产的授权
SystemUserAuthInfoURL
=
"/api/assets/v1/system-user/%s/auth-info/"
// 该系统用户的授权
SystemUserAuthInfoURL
=
"/api/assets/v1/system-user/%s/auth-info/"
// 该系统用户的授权
...
...
pkg/service/users.go
View file @
4bf1af8d
package
service
package
service
import
(
import
(
"fmt"
"cocogo/pkg/logger"
"cocogo/pkg/model"
"cocogo/pkg/model"
)
)
func
Authenticate
(
username
,
password
,
publicKey
,
remoteAddr
,
loginType
string
)
*
model
.
User
{
func
Authenticate
(
username
,
password
,
publicKey
,
remoteAddr
,
loginType
string
)
(
user
model
.
User
)
{
return
&
model
.
User
{
Id
:
"1111111111"
,
Username
:
"admin"
,
Name
:
"广宏伟"
}
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
)
{
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
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
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment