Skip to content

Commit

Permalink
Merge pull request #18 from tellusxdp/feat/add-v2
Browse files Browse the repository at this point in the history
プロバイダ名・商品識別コード・商品ラベルを廃止して、商品IDに移行
  • Loading branch information
metakoma authored Dec 24, 2020
2 parents bd794ad + f726ac8 commit 3240fd0
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 45 deletions.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ config.ymlにて以下の項目を設定します
| upsteram.url | 認証後に接続するサーバ | https://www.example.com/ |
| upstream.headers | プロキシ先に付与するリクエストヘッダ | {"Authorization": "Bearer token"} |
| private_key_url | JWTを検証する公開鍵をダウンロードするURL | https://sdk.tellusxdp.com/api/manager/v1/auth/public_keys |
| counter_url | APIリクエストのメータリング情報を登録するURL | https://sdk.tellusxdp.com/api/manager/v1/items/counts |
| upstream | 認証後に接続するサーバ | https://www.example.com/ |
| provider_id | プロバイダ名 | provider-a |
| tool_id | 商品ID | 1_9ffc0bb13148c605795b5bc22143b7b00c30ad |
| api_key | 集計用APIキー | fa3a3293-d1be-41cf-9b6a-70d4d75c41ba |
| tool_label | 商品ラベル | product01 |
| product_id | 商品ID | 366bbedd-8bc3-4374-9253-cd07f763f2bc |
| allowed_auth_types | 許可認証方式 | ["password", "apikey"] |


### Example

```yaml
Expand All @@ -45,11 +43,9 @@ upstream:

private_key_url: https://sdk.tellusxdp.com/api/manager/v1/auth/public_keys
counter_url: https://sdk.tellusxdp.com/api/manager/v1/items/counts
api_key: b424a335-ea26-4ff1-bdf8-168469778499

provider_id: acmeinc
tool_label: owesome-api
tool_id: 1_e849acf73765b19fd700af9374ab0fa2
api_key: fa3a3293-d1be-41cf-9b6a-70d4d75c41ba
product_id: 366bbedd-8bc3-4374-9253-cd07f763f2bc
allowed_auth_types:
- apikey
- password
Expand Down
6 changes: 2 additions & 4 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ http:

private_key_url: https://sdk.tellusxdp.com/api/manager/v1/auth/public_keys
counter_url: https://sdk.tellusxdp.com/api/manager/v1/items/counts
api_key: 7b4d73b1-425b-4082-897c-ba3c81962272

upstream:
url: https://www.sakura.ad.jp
headers:
X-Test-Header: hogetan

provider_id: fukuyoshi-jiro
tool_label: weather-api
tool_id: 1_9ffc0bb13148c605795b5bc22143b7b00c30ad
product_id: 9dc372e2-5819-4953-a696-5a599e760dc3
api_key: 7b4d73b1-425b-4082-897c-ba3c81962272
allowed_auth_types:
- apikey
- password
4 changes: 1 addition & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ type Config struct {
CounterURL string `yaml:"counter_url"`
APIKey string `yaml:"api_key"`
Upstream Upstream `yaml:"upstream"`
ProviderID string `yaml:"provider_id"`
ToolLabel string `yaml:"tool_label"`
ToolID string `yaml:"tool_id"`
ProductID string `yaml:"product_id"`
AllowedAuthTypes []string `yaml:"allowed_auth_types"`
}

Expand Down
6 changes: 2 additions & 4 deletions files/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ counter_url: https://sdk.tellusxdp.com/api/manager/v1/items/counts
upstream:
url: http://127.0.0.1:3000

provider_name: providername
tool_label: toollabel
tool_id: toolid
api_key: apikey
product_id: 商品ID
api_key: 集計用APIキー
allowed_auth_types:
- apikey
- password
2 changes: 1 addition & 1 deletion server/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type CountRequest struct {
ToolID string `json:"tool_id"`
ProductID string `json:"product_id"`
UserID string `json:"user_id"`
Token string `json:"token"`
RequestID string `json:"request_id"`
Expand Down
18 changes: 7 additions & 11 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func New(cfg *config.Config) (*Server, error) {
s := &Server{
Config: cfg,
Upstream: u,
Logger: log.WithField("tool_id", cfg.ToolID),
Logger: log.WithField("product_id", cfg.ProductID),
}
s.CounterChan = s.StartCountRequestLoop()
return s, nil
Expand Down Expand Up @@ -119,15 +119,11 @@ func (s *Server) configHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)

type configResponse struct {
ProviderID string `yaml:"provider_id"`
ToolLabel string `yaml:"tool_label"`
ToolID string `yaml:"tool_id"`
ProductID string `yaml:"product_id"`
AllowedAuthTypes []string `yaml:"allowed_auth_types"`
}
resp := &configResponse{
ProviderID: s.Config.ProviderID,
ToolLabel: s.Config.ToolLabel,
ToolID: s.Config.ToolID,
ProductID: s.Config.ProductID,
AllowedAuthTypes: s.Config.AllowedAuthTypes,
}

Expand Down Expand Up @@ -176,9 +172,9 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

if claim.ToolID != s.Config.ToolID {
s.Logger.Debugf("Invalid tool id %s", claim.ToolID)
writeError(w, http.StatusUnauthorized, "Unauthorized (invalid tool)")
if claim.ProductID != s.Config.ProductID {
s.Logger.Debugf("Invalid product id %s", claim.ProductID)
writeError(w, http.StatusUnauthorized, "Unauthorized (invalid product)")
return
}

Expand Down Expand Up @@ -229,7 +225,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// 有効なレスポンス
go func() {
c := CountRequest{
ToolID: s.Config.ToolID,
ProductID: s.Config.ProductID,
UserID: claim.Subject,
Token: jwtToken,
RequestID: requestID,
Expand Down
12 changes: 6 additions & 6 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ func ValidateToken(tokenString string, publicKeysURL string) (*JWTPayload, error
}

t := &JWTPayload{
Audience: claims["aud"].(string),
ID: claims["jti"].(string),
Issuer: claims["iss"].(string),
Subject: claims["sub"].(string),
ToolID: claims["tool_id"].(string),
AuthType: claims["auth_type"].(string),
Audience: claims["aud"].(string),
ID: claims["jti"].(string),
Issuer: claims["iss"].(string),
Subject: claims["sub"].(string),
ProductID: claims["product_id"].(string),
AuthType: claims["auth_type"].(string),
}
return t, nil
}
12 changes: 6 additions & 6 deletions token/type.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package token

type JWTPayload struct {
Audience string `json:"aud,omitempty"`
ID string `json:"jti,omitempty"`
Issuer string `json:"iss,omitempty"`
Subject string `json:"sub,omitempty"`
ToolID string `json:"tool_id"`
AuthType string `json:"auth_type"`
Audience string `json:"aud,omitempty"`
ID string `json:"jti,omitempty"`
Issuer string `json:"iss,omitempty"`
Subject string `json:"sub,omitempty"`
ProductID string `json:"product_id"`
AuthType string `json:"auth_type"`
}
4 changes: 2 additions & 2 deletions tools/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

def get_tool_token(token):
resp = requests.post(
"https://sdk.tellusxdp.com/api/manager/v1/auth/api_access_token/token",
json={"provider_id": "fukuyoshi-jiro", "tool_label": "weather-api"},
"https://sdk.tellusxdp.com/api/manager/v2/auth/token/",
json={"product_id": "2f59c093-4e80-419a-8584-dcee6589d3d2"},
headers={"Authorization": "Bearer {}".format(token)}
)
resp.raise_for_status()
Expand Down

0 comments on commit 3240fd0

Please sign in to comment.