Skip to content

Commit 04e012d

Browse files
authored
Merge pull request #545 from traPtitech/breaking/use-common-traq-access-token
traQのAPIアクセスにサーバー設定のAccessTokenを使う
2 parents a9a665d + 3ed7903 commit 04e012d

File tree

10 files changed

+41
-52
lines changed

10 files changed

+41
-52
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ TOKEN_KEY=
99
KNOQ_VERSION=
1010
KNOQ_REVISION=
1111
DEVELOPMENT=
12+
TRAQ_ACCESS_TOKEN=

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ debug
77

88
# for development
99
_development/*
10+
.env

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ knoQ の全ての機能を動作させるためには、追加の情報が必要
4444
| KNOQ_VERSION | 環境変数 | UNKNOWN | knoQ のバージョン (github actions でイメージ作成時に指定) |
4545
| KNOQ_REVISION | 環境変数 | UNKNOWN | git の sha1 (github actions でイメージ作成時に指定) |
4646
| DEVELOPMENT | 環境変数 | | 開発時かどうか |
47+
| TRAQ_ACCESS_TOKEN | 環境変数 | | traQ へのアクセストークン |
4748
| service.json | ファイル | 空のファイル | google calendar api に必要(権限は必要なし) |
4849

4950
### テスト

compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ services:
2525
KNOQ_VERSION: ${KNOQ_VERSION:-dev}
2626
DEVELOPMENT: true
2727
GORM_LOG_LEVEL: info
28+
TRAQ_ACCESS_TOKEN:
2829
ports:
2930
- "${APP_PORT:-3000}:3000"
3031
depends_on:

infra/traq/group.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
"github.com/traPtitech/go-traq"
1010
)
1111

12-
func (repo *TraQRepository) GetGroup(token *oauth2.Token, groupID uuid.UUID) (*traq.UserGroup, error) {
13-
ctx := context.TODO()
14-
apiClient := NewAPIClient(ctx, token)
12+
func (repo *TraQRepository) GetGroup(groupID uuid.UUID) (*traq.UserGroup, error) {
13+
ctx := context.WithValue(context.TODO(), traq.ContextAccessToken, repo.ServerAccessToken)
14+
apiClient := traq.NewAPIClient(traqAPIConfig)
15+
// TODO: 一定期間キャッシュする
1516
group, resp, err := apiClient.GroupApi.GetUserGroup(ctx, groupID.String()).Execute()
1617
if err != nil {
1718
return nil, err
@@ -23,9 +24,10 @@ func (repo *TraQRepository) GetGroup(token *oauth2.Token, groupID uuid.UUID) (*t
2324
return group, err
2425
}
2526

26-
func (repo *TraQRepository) GetAllGroups(token *oauth2.Token) ([]traq.UserGroup, error) {
27-
ctx := context.TODO()
28-
apiClient := NewAPIClient(ctx, token)
27+
func (repo *TraQRepository) GetAllGroups() ([]traq.UserGroup, error) {
28+
ctx := context.WithValue(context.TODO(), traq.ContextAccessToken, repo.ServerAccessToken)
29+
apiClient := traq.NewAPIClient(traqAPIConfig)
30+
// TODO: 一定期間キャッシュする
2931
groups, resp, err := apiClient.GroupApi.GetUserGroups(ctx).Execute()
3032
if err != nil {
3133
return nil, err
@@ -39,7 +41,7 @@ func (repo *TraQRepository) GetAllGroups(token *oauth2.Token) ([]traq.UserGroup,
3941

4042
func (repo *TraQRepository) GetUserBelongingGroupIDs(token *oauth2.Token, userID uuid.UUID) ([]uuid.UUID, error) {
4143
ctx := context.TODO()
42-
apiClient := NewAPIClient(ctx, token)
44+
apiClient := NewOauth2APIClient(ctx, token)
4345
user, resp, err := apiClient.UserApi.GetUser(ctx, userID.String()).Execute()
4446
if err != nil {
4547
return nil, err

infra/traq/traq.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import (
1414

1515
// TraQRepository is traq
1616
type TraQRepository struct {
17-
Config *oauth2.Config
18-
URL string
17+
Config *oauth2.Config
18+
URL string
19+
ServerAccessToken string
1920
}
2021

2122
var TraQDefaultConfig = &oauth2.Config{
@@ -29,6 +30,8 @@ var TraQDefaultConfig = &oauth2.Config{
2930
},
3031
}
3132

33+
var traqAPIConfig = traq.NewConfiguration()
34+
3235
func newPKCE() (pkceOptions []oauth2.AuthCodeOption, codeVerifier string) {
3336
codeVerifier = random.AlphaNumeric(43, true)
3437
result := sha256.Sum256([]byte(codeVerifier))
@@ -64,8 +67,8 @@ func (repo *TraQRepository) GetOAuthToken(query, state, codeVerifier string) (*o
6467
return repo.Config.Exchange(ctx, code, option)
6568
}
6669

67-
func NewAPIClient(ctx context.Context, token *oauth2.Token) *traq.APIClient {
68-
traqconf := traq.NewConfiguration()
70+
func NewOauth2APIClient(ctx context.Context, token *oauth2.Token) *traq.APIClient {
71+
traqconf := traqAPIConfig
6972
conf := TraQDefaultConfig
7073
traqconf.HTTPClient = conf.Client(ctx, token)
7174
apiClient := traq.NewAPIClient(traqconf)

infra/traq/user.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"golang.org/x/oauth2"
99
)
1010

11-
func (repo *TraQRepository) GetUser(token *oauth2.Token, userID uuid.UUID) (*traq.User, error) {
12-
ctx := context.TODO()
13-
apiClient := NewAPIClient(ctx, token)
11+
func (repo *TraQRepository) GetUser(userID uuid.UUID) (*traq.User, error) {
12+
ctx := context.WithValue(context.TODO(), traq.ContextAccessToken, repo.ServerAccessToken)
13+
apiClient := traq.NewAPIClient(traqAPIConfig)
14+
// TODO: 一定期間キャッシュする
1415
userDetail, resp, err := apiClient.UserApi.GetUser(ctx, userID.String()).Execute()
1516
if err != nil {
1617
return nil, err
@@ -31,9 +32,10 @@ func (repo *TraQRepository) GetUser(token *oauth2.Token, userID uuid.UUID) (*tra
3132
return &user, err
3233
}
3334

34-
func (repo *TraQRepository) GetUsers(token *oauth2.Token, includeSuspended bool) ([]traq.User, error) {
35-
ctx := context.TODO()
36-
apiClient := NewAPIClient(ctx, token)
35+
func (repo *TraQRepository) GetUsers(includeSuspended bool) ([]traq.User, error) {
36+
ctx := context.WithValue(context.TODO(), traq.ContextAccessToken, repo.ServerAccessToken)
37+
apiClient := traq.NewAPIClient(traqAPIConfig)
38+
// TODO: 一定期間キャッシュする
3739
users, resp, err := apiClient.UserApi.GetUsers(ctx).IncludeSuspended(includeSuspended).Execute()
3840
if err != nil {
3941
return nil, err
@@ -47,7 +49,7 @@ func (repo *TraQRepository) GetUsers(token *oauth2.Token, includeSuspended bool)
4749

4850
func (repo *TraQRepository) GetUserMe(token *oauth2.Token) (*traq.User, error) {
4951
ctx := context.TODO()
50-
apiClient := NewAPIClient(ctx, token)
52+
apiClient := NewOauth2APIClient(ctx, token)
5153
userDetail, resp, err := apiClient.MeApi.GetMe(ctx).Execute()
5254
if err != nil {
5355
return nil, err

main.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ var (
4343
webhookSecret = getenv("WEBHOOK_SECRET", "")
4444
activityChannelID = getenv("ACTIVITY_CHANNEL_ID", "")
4545
dailyChannelID = getenv("DAILY_CHANNEL_ID", "")
46+
47+
// TODO: traQにClient Credential Flowが実装されたら定期的に取得するように変更する
48+
// Issue: https://github.com/traPtitech/traQ/issues/2403
49+
traqAccessToken = getenv("TRAQ_ACCESS_TOKEN", "")
4650
)
4751

4852
func main() {
@@ -66,7 +70,8 @@ func main() {
6670
TokenURL: "https://q.trap.jp/api/v3/oauth2/token",
6771
},
6872
},
69-
URL: "https://q.trap.jp/api/v3",
73+
URL: "https://q.trap.jp/api/v3",
74+
ServerAccessToken: traqAccessToken,
7075
}
7176
repo := &repository.Repository{
7277
GormRepo: gormRepo,

repository/group.go

+3-16
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,7 @@ func (repo *Repository) GetGroup(groupID uuid.UUID, info *domain.ConInfo) (*doma
7474
}
7575

7676
// traq group
77-
t, err := repo.GormRepo.GetToken(info.ReqUserID)
78-
if err != nil {
79-
return nil, defaultErrorHandling(err)
80-
}
81-
g, err := repo.TraQRepo.GetGroup(t, groupID)
77+
g, err := repo.TraQRepo.GetGroup(groupID)
8278
if err != nil {
8379
return nil, defaultErrorHandling(err)
8480
}
@@ -95,16 +91,12 @@ func (repo *Repository) GetGroup(groupID uuid.UUID, info *domain.ConInfo) (*doma
9591

9692
func (repo *Repository) GetAllGroups(info *domain.ConInfo) ([]*domain.Group, error) {
9793
groups := make([]*domain.Group, 0)
98-
t, err := repo.GormRepo.GetToken(info.ReqUserID)
99-
if err != nil {
100-
return nil, defaultErrorHandling(err)
101-
}
10294
gg, err := repo.GormRepo.GetAllGroups()
10395
if err != nil {
10496
return nil, defaultErrorHandling(err)
10597
}
10698
groups = append(groups, db.ConvSPGroupToSPdomainGroup(gg)...)
107-
tg, err := repo.TraQRepo.GetAllGroups(t)
99+
tg, err := repo.TraQRepo.GetAllGroups()
108100
if err != nil {
109101
return nil, defaultErrorHandling(err)
110102
}
@@ -195,12 +187,7 @@ func (repo *Repository) getTraPGroup(info *domain.ConInfo) *domain.Group {
195187
}
196188

197189
func (repo *Repository) GetGradeGroupNames(info *domain.ConInfo) ([]string, error) {
198-
t, err := repo.GormRepo.GetToken(info.ReqUserID)
199-
if err != nil {
200-
return nil, defaultErrorHandling(err)
201-
}
202-
203-
groups, err := repo.TraQRepo.GetAllGroups(t)
190+
groups, err := repo.TraQRepo.GetAllGroups()
204191
if err != nil {
205192
return nil, defaultErrorHandling(err)
206193
}

repository/user.go

+3-17
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ func (repo *Repository) SyncUsers(info *domain.ConInfo) error {
1717
if !repo.IsPrivilege(info) {
1818
return domain.ErrForbidden
1919
}
20-
t, err := repo.GormRepo.GetToken(info.ReqUserID)
21-
if err != nil {
22-
return defaultErrorHandling(err)
23-
}
24-
traQUsers, err := repo.TraQRepo.GetUsers(t, true)
20+
traQUsers, err := repo.TraQRepo.GetUsers(true)
2521
if err != nil {
2622
return defaultErrorHandling(err)
2723
}
@@ -92,18 +88,13 @@ func (repo *Repository) LoginUser(query, state, codeVerifier string) (*domain.Us
9288
}
9389

9490
func (repo *Repository) GetUser(userID uuid.UUID, info *domain.ConInfo) (*domain.User, error) {
95-
t, err := repo.GormRepo.GetToken(info.ReqUserID)
96-
if err != nil {
97-
return nil, defaultErrorHandling(err)
98-
}
99-
10091
userMeta, err := repo.GormRepo.GetUser(userID)
10192
if err != nil {
10293
return nil, defaultErrorHandling(err)
10394
}
10495

10596
if userMeta.Provider.Issuer == traQIssuerName {
106-
userBody, err := repo.TraQRepo.GetUser(t, userID)
97+
userBody, err := repo.TraQRepo.GetUser(userID)
10798
if err != nil {
10899
return nil, defaultErrorHandling(err)
109100
}
@@ -120,17 +111,12 @@ func (repo *Repository) GetUserMe(info *domain.ConInfo) (*domain.User, error) {
120111
}
121112

122113
func (repo *Repository) GetAllUsers(includeSuspend, includeBot bool, info *domain.ConInfo) ([]*domain.User, error) {
123-
t, err := repo.GormRepo.GetToken(info.ReqUserID)
124-
if err != nil {
125-
return nil, defaultErrorHandling(err)
126-
}
127-
128114
userMetas, err := repo.GormRepo.GetAllUsers(!includeSuspend)
129115
if err != nil {
130116
return nil, defaultErrorHandling(err)
131117
}
132118
// TODO fix
133-
traQUserBodys, err := repo.TraQRepo.GetUsers(t, includeSuspend)
119+
traQUserBodys, err := repo.TraQRepo.GetUsers(includeSuspend)
134120
if err != nil {
135121
return nil, defaultErrorHandling(err)
136122
}

0 commit comments

Comments
 (0)