-
Notifications
You must be signed in to change notification settings - Fork 5
/
service.go
63 lines (56 loc) · 1.75 KB
/
service.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
package firebase
import (
"context"
"fmt"
"github.com/viant/scy"
"github.com/viant/scy/auth"
"github.com/viant/scy/auth/firebase"
"github.com/viant/scy/auth/gcp"
"github.com/viant/scy/auth/gcp/client"
"github.com/viant/scy/auth/jwt"
"github.com/viant/scy/cred"
"google.golang.org/api/option"
"reflect"
)
type (
Service struct {
firebase *firebase.Service
}
)
func (s *Service) BasicAuth(ctx context.Context, user string, password string) (*auth.Token, error) {
return s.firebase.InitiateBasicAuth(ctx, user, password)
}
func (s *Service) VerifyIdentity(ctx context.Context, idToken string) (*jwt.Claims, error) {
return s.verifyToken(ctx, idToken)
}
func (s *Service) verifyToken(ctx context.Context, idToken string) (*jwt.Claims, error) {
if claims, err := gcp.JwtClaims(ctx, idToken); err == nil {
return claims, nil
}
return s.firebase.VerifyIdentity(ctx, idToken)
}
func New(ctx context.Context, config *Config) (*Service, error) {
var options []option.ClientOption
if resource := config.Resource; resource != nil && resource.URL != "" {
scyService := scy.New()
resource.SetTarget(reflect.TypeOf(&cred.Generic{}))
secret, err := scyService.Load(ctx, resource)
if err != nil {
return nil, err
}
fmt.Printf("using firebase with cred")
options = append(options, option.WithCredentialsJSON([]byte(secret.String())))
} else {
gcpService := gcp.New(client.NewScy())
fmt.Printf("using firebase with source token")
tokenSource := gcpService.TokenSource("https://www.googleapis.com/auth/cloud-platform")
options = append(options, option.WithTokenSource(tokenSource))
}
firebaseService, err := firebase.New(ctx, config.Config, options...)
if err != nil {
return nil, err
}
return &Service{
firebase: firebaseService,
}, nil
}