Skip to content

Commit

Permalink
Merge pull request #42 from ee0703/develop
Browse files Browse the repository at this point in the history
为SDK添加App Grant access接口
  • Loading branch information
nowenL authored Dec 14, 2016
2 parents 46d0a49 + 9af6808 commit e45ae34
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
27 changes: 27 additions & 0 deletions kirksdk/account_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ type AccountClient interface {

// GetQcosClient 用于得到与某个 App 交互的 QcosClient
GetQcosClient(ctx context.Context, appURI string) (client QcosClient, err error)

// CreateAppGrant 将应用授权给用户
CreateAppGrant(ctx context.Context, appURI, username string) (err error)

// DeleteAppGrant 删除应用授权
DeleteAppGrant(ctx context.Context, appURI, username string) (err error)

// ListAppGrantedUsers 列出应用已授权的用户列表
ListAppGrantedUsers(ctx context.Context, appURI string) (ret []AppGrantedUser, err error)

// ListGrantedApps 列出已被授权的应用
ListGrantedApps(ctx context.Context) (ret []AppInfo, err error)

// GetGrantedAppKey 获取被授权应用的key
GetGrantedAppKey(ctx context.Context, appURI string) (ret GrantedAppKey, err error)
}

// AccountConfig 包含创建 AccountClient 所需的信息
Expand Down Expand Up @@ -178,3 +193,15 @@ type UpdateAlertMethodArgs struct {
Nationality string `json:"nationality"`
Code string `json:"code"`
}

// AppGrantedUser 包含列出应用被授权的用户信息
type AppGrantedUser struct {
ID uint32 `json:"id"`
Name string `json:"name"`
}

// GrantedAppKey 包含被授权应用的key信息
type GrantedAppKey struct {
Ak string `json:"ak"`
Sk string `json:"sk"`
}
82 changes: 76 additions & 6 deletions kirksdk/account_client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package kirksdk

import (
"errors"
"fmt"
"net/http"
"strings"

"golang.org/x/net/context"
"qiniupkg.com/kirk/kirksdk/mac"
Expand All @@ -11,6 +13,8 @@ import (

const appVersionPrefix = "/v3"

var ErrInvalidAppURI = errors.New("app uri is invalid")

type accountClientImp struct {
accessKey string
secretKey string
Expand Down Expand Up @@ -133,6 +137,36 @@ func (p *accountClientImp) UpdateAlertMethod(ctx context.Context, appURI string,
return
}

func (p *accountClientImp) CreateAppGrant(ctx context.Context, appURI, username string) (err error) {
url := fmt.Sprintf("%s%s/apps/%s/grant/%s", p.host, appVersionPrefix, appURI, username)
err = p.client.Call(ctx, nil, "PUT", url)
return
}

func (p *accountClientImp) DeleteAppGrant(ctx context.Context, appURI, username string) (err error) {
url := fmt.Sprintf("%s%s/apps/%s/grant/%s", p.host, appVersionPrefix, appURI, username)
err = p.client.Call(ctx, nil, "DELETE", url)
return
}

func (p *accountClientImp) ListAppGrantedUsers(ctx context.Context, appURI string) (ret []AppGrantedUser, err error) {
url := fmt.Sprintf("%s%s/apps/%s/grants", p.host, appVersionPrefix, appURI)
err = p.client.Call(ctx, &ret, "GET", url)
return
}

func (p *accountClientImp) ListGrantedApps(ctx context.Context) (ret []AppInfo, err error) {
url := fmt.Sprintf("%s%s/granted", p.host, appVersionPrefix)
err = p.client.Call(ctx, &ret, "GET", url)
return
}

func (p *accountClientImp) GetGrantedAppKey(ctx context.Context, appURI string) (ret GrantedAppKey, err error) {
url := fmt.Sprintf("%s%s/granted/%s/key", p.host, appVersionPrefix, appURI)
err = p.client.Call(ctx, &ret, "GET", url)
return
}

func (p *accountClientImp) GetIndexClient(ctx context.Context) (client IndexClient, err error) {
accountInfo, err := p.GetAccountInfo(ctx)
if err != nil {
Expand Down Expand Up @@ -163,12 +197,21 @@ func (p *accountClientImp) GetQcosClient(ctx context.Context, appURI string) (cl
err error
}

keyChan := make(chan keyResult)
endpointChan := make(chan endpointResult)
// app uri should follow the format: "username.appname"
// or it will return an invalid app uri
appURIParts := strings.Split(appURI, ".")
if len(appURIParts) < 2 {
return nil, ErrInvalidAppURI
}

// Get app access key & secret key
go func() {
var result keyResult
// check if app is granted
accountInfo, err := p.GetAccountInfo(ctx)
if err != nil {
return
}
isGranted := (accountInfo.Name != appURIParts[0])

getAppKeyFunc := func() (result keyResult) {
keyPairs, err := p.GetAppKeys(ctx, appURI)
if err != nil {
result.err = err
Expand All @@ -182,6 +225,33 @@ func (p *accountClientImp) GetQcosClient(ctx context.Context, appURI string) (cl
}
}
}
return
}

getGrantedAppKeyFunc := func() (result keyResult) {
keyPair, err := p.GetGrantedAppKey(ctx, appURI)
result.err = err
if err == nil {
result.ak = keyPair.Ak
result.sk = keyPair.Sk
}
return
}

// set up list apps and get key func
listAppsFunc := p.ListApps
getKeyFunc := getAppKeyFunc
if isGranted {
listAppsFunc = p.ListGrantedApps
getKeyFunc = getGrantedAppKeyFunc
}

keyChan := make(chan keyResult)
endpointChan := make(chan endpointResult)

// Get app access key & secret key
go func() {
result := getKeyFunc()

if result.ak == "" {
result.err = fmt.Errorf("Fail to find keys for app \"%s\"", appURI)
Expand All @@ -193,7 +263,7 @@ func (p *accountClientImp) GetQcosClient(ctx context.Context, appURI string) (cl
// Get qcos end point
go func() {
var result endpointResult
appInfos, err := p.ListApps(ctx)
appInfos, err := listAppsFunc(ctx)
if err != nil {
result.err = err
endpointChan <- result
Expand Down

0 comments on commit e45ae34

Please sign in to comment.