-
Notifications
You must be signed in to change notification settings - Fork 9
/
signin.go
147 lines (115 loc) · 3.73 KB
/
signin.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package fnapi
import (
"context"
"time"
"namespacelabs.dev/foundation/internal/clerk"
"namespacelabs.dev/foundation/internal/fnerrors"
)
type StartLoginRequest struct {
Kind string `json:"kind"`
}
type StartLoginResponse struct {
LoginId string `json:"login_id"`
LoginUrl string `json:"login_url"`
Kind string `json:"kind"`
}
type CompleteLoginRequest struct {
LoginId string `json:"login_id"`
EphemeralCliId string `json:"ephemeral_cli_id"`
}
type CheckRequest struct {
UserData string `json:"userData"`
}
type RobotLoginRequest struct {
Repository string `json:"repository"`
AccessToken string `json:"accessToken"`
}
type GetSessionTokenRequest struct {
UserData string `json:"user_data"`
DurationSeconds uint32 `json:"duration_seconds"`
}
type GetSessionTokenResponse struct {
Token string `json:"token"`
Expiration time.Time `json:"expiration"`
}
// Returns the URL which the user should open.
func StartLogin(ctx context.Context, kind string) (*StartLoginResponse, error) {
req := StartLoginRequest{Kind: kind}
var resp StartLoginResponse
if err := AnonymousCall(ctx, EndpointAddress, "nsl.signin.SigninService/StartLogin", req, DecodeJSONResponse(&resp)); err != nil {
return nil, err
}
if resp.LoginUrl == "" {
return nil, fnerrors.InternalError("bad login response")
}
return &resp, nil
}
func CompleteLogin(ctx context.Context, id, kind string, ephemeralCliId string) (*UserAuth, error) {
if kind == "clerk" {
t, err := completeClerkLogin(ctx, id, ephemeralCliId)
if err != nil {
return nil, err
}
n, err := clerk.Login(ctx, t.Ticket)
if err != nil {
return nil, err
}
return &UserAuth{
Username: n.Email,
Clerk: n,
}, nil
}
req := CompleteLoginRequest{
LoginId: id,
EphemeralCliId: ephemeralCliId,
}
method := "nsl.signin.SigninService/CompleteLogin"
var resp []UserAuth
// Explicitly use CallAPI() so we don't surface an action to the user while waiting.
if err := AnonymousCall(ctx, EndpointAddress, method, req, DecodeJSONResponse(&resp)); err != nil {
return nil, err
}
if len(resp) != 1 {
return nil, fnerrors.InternalError("expected exactly one response (got %d)", len(resp))
}
return &resp[0], nil
}
type CompleteClerkLoginResponse struct {
Ticket string `json:"ticket,omitempty"`
}
func completeClerkLogin(ctx context.Context, id string, ephemeralCliId string) (*CompleteClerkLoginResponse, error) {
req := CompleteLoginRequest{
LoginId: id,
EphemeralCliId: ephemeralCliId,
}
method := "nsl.signin.SigninService/CompleteClerkLogin"
var resp []CompleteClerkLoginResponse
// Explicitly use CallAPI() so we don't surface an action to the user while waiting.
if err := AnonymousCall(ctx, EndpointAddress, method, req, DecodeJSONResponse(&resp)); err != nil {
return nil, err
}
if len(resp) != 1 {
return nil, fnerrors.InternalError("expected exactly one response (got %d)", len(resp))
}
return &resp[0], nil
}
func CheckSignin(ctx context.Context, userData string) (*UserAuth, error) {
req := CheckRequest{
UserData: userData,
}
userAuth := &UserAuth{}
err := AnonymousCall(ctx, EndpointAddress, "nsl.signin.SigninService/Check", req, DecodeJSONResponse(userAuth))
return userAuth, err
}
func RobotLogin(ctx context.Context, repository, accessToken string) (*UserAuth, error) {
req := RobotLoginRequest{
Repository: repository,
AccessToken: accessToken,
}
userAuth := &UserAuth{}
err := AnonymousCall(ctx, EndpointAddress, "nsl.signin.SigninService/RobotLogin", req, DecodeJSONResponse(userAuth))
return userAuth, err
}