This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservices.go
192 lines (149 loc) · 4.98 KB
/
services.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2019 Christian Banse
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package issues
import (
"context"
"database/sql"
"errors"
"fmt"
"net/http"
"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/v29/github"
"github.com/gregjones/httpcache"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
// TokenCache contains a simple cache of github clients
var clients map[int64]*GitHubClients
var installationClients map[int64]*GitHubClients
var ErrAuthenticationNeeded = errors.New("You need to authenticate with the service")
const ServiceGitHub = "GitHub"
// GitHubClients is a structure that provides v3 (REST) and v4 (GraphQL) GitHub clients
type GitHubClients struct {
// The authenticated user, if this is a user-client
User *github.User
// Specifices, if this is a user client or installation client
IsUserClient bool
V3 *github.Client
V4 *githubv4.Client
}
// ServiceToken represents an OAuth2-style token to an external service, such as GitHub
type ServiceToken struct {
UserID int64 `json:"userID" gorm:"primary_key"`
Service string `json:"service"`
AccessToken string `json:"accessToken"`
}
func (app *Application) newGitHubClients(userID int64) (clients *GitHubClients, err error) {
var (
serviceToken *ServiceToken
)
// fetch token from db
if serviceToken, err = app.db.GetServiceToken("GitHub", userID); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrAuthenticationNeeded
}
return nil, fmt.Errorf("Could not fetch GitHub token from database: %w", err)
}
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: serviceToken.AccessToken,
})
cachedTransport := httpcache.NewMemoryCacheTransport()
cachedTransport.Transport = &oauth2.Transport{
Source: tokenSource,
}
httpClient := &http.Client{Transport: cachedTransport}
// creating new GitHub clients
clients = &GitHubClients{
IsUserClient: true,
V3: github.NewClient(httpClient),
V4: githubv4.NewClient(httpClient),
}
// fetch a user, to test the client
if clients.User, _, err = clients.V3.Users.Get(context.Background(), ""); err != nil {
return nil, fmt.Errorf("Could not create GitHub clients because authenticated user could not be retrieved: %s", err)
}
log.Debugf("Succesfully created GitHub clients for authenticated user %s", clients.User.GetLogin())
return clients, nil
}
func (app *Application) newGitHubInstallationClients(installationID int64) (clients *GitHubClients, err error) {
tr := http.DefaultTransport
itr, err := ghinstallation.NewKeyFromFile(tr, app.AppID, installationID, "keys/private-key.pem")
if err != nil {
return nil, err
}
httpClient := &http.Client{Transport: itr}
// creating new GitHub clients
clients = &GitHubClients{
IsUserClient: false,
V3: github.NewClient(httpClient),
V4: githubv4.NewClient(httpClient),
}
log.Debugf("Succesfully created GitHub clients for installation ID %d", installationID)
return
}
func init() {
clients = make(map[int64]*GitHubClients)
installationClients = make(map[int64]*GitHubClients)
}
func (app *Application) AddServiceToken(token *ServiceToken) (err error) {
var old *ServiceToken
// check, if it already exists
old, err = app.GetServiceToken(token.Service, token.UserID)
if err != nil {
return err
}
if old == nil {
return app.db.Insert(token)
}
old.AccessToken = token.AccessToken
_, err = app.db.Update(token)
// force in-memory cache to refresh
if token.Service == ServiceGitHub {
delete(clients, token.UserID)
}
return
}
func (app *Application) GetServiceToken(service string, userID int64) (token *ServiceToken, err error) {
return app.db.GetServiceToken(service, userID)
}
func (app *Application) GetUserClients(userID int64) (c *GitHubClients, err error) {
var (
found bool
)
c, found = clients[userID]
if found {
log.Debugf("Using in-memory GitHub clients for authenticated user %s", c.User.GetLogin())
return c, nil
}
if c, err = app.newGitHubClients(userID); err != nil {
return nil, err
}
clients[userID] = c
return
}
func (app *Application) GetInstallationClients(installationID int64) (c *GitHubClients, err error) {
var (
found bool
)
c, found = installationClients[installationID]
if found {
log.Debugf("Using in-memory GitHub clients for installation %d", installationID)
return c, nil
}
if c, err = app.newGitHubInstallationClients(installationID); err != nil {
return nil, err
}
installationClients[installationID] = c
return
}