-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
87 lines (79 loc) · 2.56 KB
/
identity.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
// Package identity that handles signup, reset password, verification of email etc.
// This is an admin package be careful while using these functions .....
package identity
import (
"context"
"fmt"
"io/ioutil"
"os"
firebase "firebase.google.com/go"
"firebase.google.com/go/auth"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
)
// IDP ... client managing email/password related authorization
type IDP struct {
projectID string
client *auth.Client
fireApp *firebase.App
}
// NewIDPEP .... intializes firebase auth which will do al sorts of authn/authz
func NewIDPEP(ctx context.Context, projectID string) (*IDP, error) {
serviceAccountSD := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
if serviceAccountSD == "" {
return nil, fmt.Errorf("Missing service account file for backend server")
}
targetScopes := []string{
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/userinfo.email",
}
currentCreds, _, err := readCredentialsFile(ctx, serviceAccountSD, targetScopes)
opt := option.WithCredentials(currentCreds)
app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
return nil, fmt.Errorf("Failed to initialize identity client")
}
currentClient, err := app.Auth(ctx)
if err != nil {
return nil, fmt.Errorf("Failed to initialize identity client")
}
return &IDP{projectID: projectID, client: currentClient, fireApp: app}, nil
}
// VerifyUserToken ....
func (id *IDP) VerifyUserToken(ctx context.Context, idToken string) (*auth.Token, error) {
verificationOutput, err := id.client.VerifyIDToken(ctx, idToken)
if err != nil {
return nil, err
}
return verificationOutput, nil
}
// ResetUserPassword ...
func (id *IDP) ResetUserPassword(ctx context.Context, email string) error {
currentResetLink, err := id.client.PasswordResetLink(ctx, email)
if err != nil {
return err
}
// TODO: Implement SMTP server from GSuite/Others to send out custom emails
// Would also need HTML template for the same
fmt.Println(currentResetLink)
return nil
}
// GetUserByEmail ...
func (id *IDP) GetUserByEmail(ctx context.Context, email string) (*auth.UserRecord, error) {
currentUser, err := id.client.GetUserByEmail(ctx, email)
if err != nil {
return nil, err
}
return currentUser, nil
}
func readCredentialsFile(ctx context.Context, filename string, scopes []string) (*google.Credentials, []byte, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, nil, err
}
creds, err := google.CredentialsFromJSON(ctx, b, scopes...)
if err != nil {
return nil, nil, err
}
return creds, b, nil
}