-
Notifications
You must be signed in to change notification settings - Fork 9
/
dbgap.go
362 lines (329 loc) · 10.8 KB
/
dbgap.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright 2019 Google LLC
//
// 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 translator
import (
"bytes"
"context"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"net/http"
"regexp"
"strings"
"time"
"gopkg.in/square/go-jose.v2/jwt" /* copybara-comment */
"github.com/coreos/go-oidc" /* copybara-comment */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/common" /* copybara-comment: common */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/ga4gh" /* copybara-comment: ga4gh */
)
const (
// TODO: Update the issuer address once NCBI stands up their own OIDC endpoint.
dbGapIssuer = "https://dbgap.nlm.nih.gov/aa"
dbGapOrgURL = "https://orgs.nih.gov/orgs/"
dbGapUserInfoURL = "https://dbgap.ncbi.nlm.nih.gov/aa/jwt/user_info.cgi?${TOKEN}"
dbGapPassportURL = "https://dbgap.ncbi.nlm.nih.gov/aa/jwt/user_passport.cgi?${TOKEN}"
eraCommonsAuthority = "eRA"
visaScope = "openid"
fixedKeyID = "kid"
)
// DbGapTranslator is a ga4gh.Translator that converts dbGap identities into GA4GH identities.
type DbGapTranslator struct {
publicKey *rsa.PublicKey
visaIssuer string
signingPrivateKey *rsa.PrivateKey
}
type dbGapStudy struct {
Accession *string `json:"accession"`
}
type dbGapAccess struct {
Study dbGapStudy `json:"study"`
Expires int64 `json:"expires"`
Issued int64 `json:"issued"`
}
type dbGapPassport struct {
Access []dbGapAccess `json:"access"`
Org *string `json:"org"`
OrgID *string `json:"org_DUNS"`
Role *string `json:"role"`
SO *string `json:"so"`
}
type dbGapIdentity struct {
Authority string `json:"authority"`
ID interface{} `json:"id"`
}
type vCard struct {
Email string `json:"email"`
GivenName string `json:"fname"`
FamilyName string `json:"lname"`
Orgs []string `json:"orgs"`
Roles []string `json:"roles"`
}
type dbGapClaims struct {
DbGapPassport []dbGapPassport `json:"dbgap_passport"`
Identity []dbGapIdentity `json:"identity"`
Vcard vCard `json:"vcard"`
}
// dbGapIdToken mocks OIDC library's idToken implementation, except minor differences in the types of
// Audience, Expiry, and IssuedAt fields to facilitate JSON unmarshalling.
type dbGapIdToken struct {
Issuer string `json:"iss"`
Subject string `json:"sub"`
Audience string `json:"aud"`
Expiry int64 `json:"exp"`
IssuedAt int64 `json:"iat"`
Nonce string `json:"nonce"`
AtHash string `json:"at_hash"`
}
const validSec = 3600 * 24 * 60 // 60 days
var removePunctuation = regexp.MustCompile("[^a-zA-Z0-9 ]+")
func convertToOIDCIDToken(token dbGapIdToken) *oidc.IDToken {
return &oidc.IDToken{
Issuer: token.Issuer,
Subject: token.Subject,
Audience: []string{token.Audience},
Expiry: time.Unix(token.Expiry, 0),
IssuedAt: time.Unix(token.IssuedAt, 0),
Nonce: token.Nonce,
AccessTokenHash: token.AtHash,
}
}
// NewDbGapTranslator creates a new DbGapTranslator with the provided public key. If the tokens
// passed to this translator do not have an audience claim with a value equal to the
// clientID value then they will be rejected.
func NewDbGapTranslator(publicKey, selfIssuer, signingPrivateKey string) (*DbGapTranslator, error) {
if len(selfIssuer) == 0 || len(signingPrivateKey) == 0 {
return nil, fmt.Errorf("NewDbGapTranslator failed, selfIssuer or signingPrivateKey is empty")
}
t := &DbGapTranslator{visaIssuer: selfIssuer}
block, _ := pem.Decode([]byte(signingPrivateKey))
if block == nil {
return nil, fmt.Errorf("decode private key failed")
}
pri, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("parsing private key: %v", err)
}
t.signingPrivateKey = pri
block, _ = pem.Decode([]byte(publicKey))
if block == nil {
return t, nil
}
pub, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("parsing public key: %v", err)
}
t.publicKey = pub
return t, nil
}
// TranslateToken implements the ga4gh.Translator interface.
func (s *DbGapTranslator) TranslateToken(ctx context.Context, auth string) (*ga4gh.Identity, error) {
if err := common.VerifyTokenWithKey(s.publicKey, auth); err != nil {
return nil, fmt.Errorf("verifying user token signature: %v", err)
}
userInfo, err := s.getURL(dbGapUserInfoURL, auth)
if err != nil {
return nil, fmt.Errorf("getting dbGaP user info: %v", err)
}
if err := common.VerifyTokenWithKey(s.publicKey, userInfo); err != nil {
return nil, fmt.Errorf("verifying user info token signature: %v", err)
}
passport, err := s.getURL(dbGapPassportURL, auth)
if err != nil {
return nil, fmt.Errorf("getting dbGaP passport: %v", err)
}
if err := common.VerifyTokenWithKey(s.publicKey, passport); err != nil {
return nil, fmt.Errorf("verifying passport token signature: %v", err)
}
var claims dbGapClaims
var id dbGapIdToken
if err := s.extractClaims(auth, &id, &claims); err != nil {
return nil, fmt.Errorf("extracting user claims: %v", err)
}
if err := s.extractClaims(userInfo, &id, &claims); err != nil {
return nil, fmt.Errorf("extracting user info claims: %v", err)
}
if err := s.extractClaims(passport, &id, &claims); err != nil {
return nil, fmt.Errorf("extracting passport claims: %v", err)
}
return s.translateToken(convertToOIDCIDToken(id), claims, time.Now())
}
func (s *DbGapTranslator) getURL(url, userTok string) (string, error) {
url = strings.Replace(url, "${TOKEN}", userTok, -1)
get, err := http.Get(url)
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
buf.ReadFrom(get.Body)
body := buf.String()
if get.StatusCode < 200 || get.StatusCode > 299 {
return "", fmt.Errorf("http status %d: %v", get.StatusCode, body)
}
return body, nil
}
func (s *DbGapTranslator) extractClaims(tok string, id *dbGapIdToken, claims *dbGapClaims) error {
parsed, err := jwt.ParseSigned(tok)
if err != nil {
return fmt.Errorf("parsing signed token: %v", err)
}
err = parsed.UnsafeClaimsWithoutVerification(id, claims)
if err != nil {
return fmt.Errorf("extracting claims from token: %v", err)
}
return nil
}
func (s *DbGapTranslator) translateToken(token *oidc.IDToken, claims dbGapClaims, now time.Time) (*ga4gh.Identity, error) {
id := ga4gh.Identity{
Issuer: token.Issuer,
Subject: token.Subject,
Expiry: token.Expiry.Unix(),
GivenName: claims.Vcard.GivenName,
FamilyName: claims.Vcard.FamilyName,
Name: common.JoinNonEmpty([]string{claims.Vcard.GivenName, claims.Vcard.FamilyName}, " "),
Email: claims.Vcard.Email,
VisaJWTs: []string{},
}
for _, ident := range claims.Identity {
if ident.Authority == eraCommonsAuthority {
if username, ok := ident.ID.(string); ok {
id.Username = username
}
}
}
accessions := make(map[string]dbGapAccess)
type source struct {
orgID string
by string
}
affiliations := make(map[string]source)
for _, p := range claims.DbGapPassport {
for _, a := range p.Access {
if a.Study.Accession == nil {
continue
}
// TODO: Verify that the heuristics for de-duplicating access entries is correct.
ac := *a.Study.Accession
exp := a.Expires
if access, ok := accessions[ac]; ok {
// For duplicate accessions, only keep the one with the later expiry timestamp.
if access.Expires > exp {
continue
}
}
accessions[ac] = dbGapAccess{
Expires: exp,
Issued: a.Issued,
}
}
if p.Org == nil || len(*p.Org) == 0 || p.Role == nil || len(*p.Role) == 0 {
continue
}
var r string
if *p.Role == "pi" || *p.Role == "downloader" {
r = "nih.researcher"
} else {
r = "member"
}
o := removePunctuation.ReplaceAllString(*p.Org, "")
o = strings.ReplaceAll(o, " ", "-")
v := r + "@" + o + ".orgs.nih.gov"
// Does not deal with complex cases where multiple org_DUNS attest to the same
// "value" (v) for AffiliationAndRole.
if src, ok := affiliations[v]; !ok || src.by == "self" {
by := "so"
if p.SO == nil || *p.SO == "" {
by = "self"
}
affiliations[v] = source{
orgID: *p.OrgID,
by: by,
}
}
}
currUnixTime := now.Unix()
affiliationAsserted := now.Unix()
for a, val := range accessions {
visa := ga4gh.VisaData{
StdClaims: ga4gh.StdClaims{
Subject: token.Subject,
Issuer: s.visaIssuer,
ExpiresAt: val.Expires,
IssuedAt: val.Issued,
},
Assertion: ga4gh.Assertion{
Type: ga4gh.ControlledAccessGrants,
Value: ga4gh.Value("https://dac.nih.gov/datasets/" + a),
Source: dbGapIssuer,
By: ga4gh.DAC,
Asserted: affiliationAsserted,
},
Scope: visaScope,
}
v, err := ga4gh.NewVisaFromData(&visa, ga4gh.RS256, s.signingPrivateKey, fixedKeyID)
if err != nil {
return nil, fmt.Errorf("sign ControlledAccessGrants claim failed: %s", err)
}
id.VisaJWTs = append(id.VisaJWTs, string(v.JWT()))
// Keep the oldest Issued accession for use as affiliationAsserted.
if val.Issued > 0 && val.Issued < affiliationAsserted {
affiliationAsserted = val.Issued
}
}
for a, src := range affiliations {
// Claim for dbGap
visa := ga4gh.VisaData{
StdClaims: ga4gh.StdClaims{
Issuer: s.visaIssuer,
ExpiresAt: currUnixTime + validSec,
IssuedAt: affiliationAsserted,
},
Assertion: ga4gh.Assertion{
Type: ga4gh.AffiliationAndRole,
Value: ga4gh.Value(a),
Source: dbGapIssuer,
By: ga4gh.System,
Asserted: affiliationAsserted,
},
Scope: visaScope,
}
v, err := ga4gh.NewVisaFromData(&visa, ga4gh.RS256, s.signingPrivateKey, fixedKeyID)
if err != nil {
return nil, fmt.Errorf("sign dbGap ClaimAffiliationAndRole claim failed: %s", err)
}
id.VisaJWTs = append(id.VisaJWTs, string(v.JWT()))
// Claim for org
visa = ga4gh.VisaData{
StdClaims: ga4gh.StdClaims{
Issuer: s.visaIssuer,
ExpiresAt: currUnixTime + validSec,
IssuedAt: affiliationAsserted,
},
Assertion: ga4gh.Assertion{
Type: ga4gh.AffiliationAndRole,
Value: ga4gh.Value(a),
Source: ga4gh.Source(dbGapOrgURL + src.orgID),
By: ga4gh.By(src.by),
Asserted: affiliationAsserted,
},
Scope: visaScope,
}
v, err = ga4gh.NewVisaFromData(&visa, ga4gh.RS256, s.signingPrivateKey, fixedKeyID)
if err != nil {
return nil, fmt.Errorf("sign org ClaimAffiliationAndRole claim failed: %s", err)
}
id.VisaJWTs = append(id.VisaJWTs, string(v.JWT()))
}
return &id, nil
}