-
Notifications
You must be signed in to change notification settings - Fork 17
/
google_auth.go
76 lines (63 loc) · 1.87 KB
/
google_auth.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
package auth
import (
"context"
"encoding/json"
"fmt"
"net/http"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/idtoken"
"google.golang.org/api/option"
htransport "google.golang.org/api/transport/http"
)
const (
defaultCaraMLAudience = "api.caraml"
// JSON key file types
serviceAccountKey = "service_account"
)
type credentialsFile struct {
Type string `json:"type"`
}
// idTokenSource is an oauth2.TokenSource that wraps another TokenSource
// It takes the id_token from TokenSource and passes that on as a bearer token
type idTokenSource struct {
TokenSource oauth2.TokenSource
}
func (s *idTokenSource) Token() (*oauth2.Token, error) {
token, err := s.TokenSource.Token()
if err != nil {
return nil, err
}
idToken, ok := token.Extra("id_token").(string)
if !ok {
return nil, fmt.Errorf("token did not contain an id_token")
}
return &oauth2.Token{
AccessToken: idToken,
TokenType: "Bearer",
Expiry: token.Expiry,
}, nil
}
// InitGoogleClient is a helper method to be used by CaraML components to initialise a Google Client that appends ID
// tokens to the headers of all outgoing requests with ID tokens, regardless of the type of credentials used
func InitGoogleClient(ctx context.Context) (*http.Client, error) {
cred, err := google.FindDefaultCredentials(ctx)
if err != nil {
return nil, err
}
var f credentialsFile
if err := json.Unmarshal(cred.JSON, &f); err != nil {
return nil, err
}
if f.Type == serviceAccountKey {
return idtoken.NewClient(ctx, defaultCaraMLAudience)
}
tokenSource := oauth2.ReuseTokenSource(nil, &idTokenSource{TokenSource: cred.TokenSource})
var opts []idtoken.ClientOption
opts = append(opts, option.WithTokenSource(tokenSource))
t, err := htransport.NewTransport(ctx, http.DefaultTransport, opts...)
if err != nil {
return nil, err
}
return &http.Client{Transport: t}, nil
}