forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_services_auth.go
260 lines (210 loc) · 7.18 KB
/
api_services_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
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
package bertyprotocol
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"go.uber.org/zap"
"golang.org/x/net/context/ctxhttp"
"berty.tech/berty/v2/go/internal/logutil"
"berty.tech/berty/v2/go/pkg/authtypes"
"berty.tech/berty/v2/go/pkg/bertyauth"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
"berty.tech/berty/v2/go/pkg/pushtypes"
)
func (s *service) authInitURL(baseURL string, services ...string) (string, error) {
parsedAuthURL, err := url.Parse(baseURL)
if err != nil {
return "", errcode.ErrServicesAuthInvalidURL
}
switch parsedAuthURL.Scheme {
case "http", "https":
default:
return "", errcode.ErrServicesAuthInvalidURL
}
if parsedAuthURL.Host == "" {
return "", errcode.ErrServicesAuthInvalidURL
}
baseURL = strings.TrimSuffix(baseURL, "/")
auth, codeChallenge, err := bertyauth.NewAuthSession(baseURL)
if err != nil {
return "", err
}
s.authSession.Store(auth)
outURL := fmt.Sprintf("%s%s?response_type=%s&client_id=%s&redirect_uri=%s&state=%s&code_challenge=%s&code_challenge_method=%s",
baseURL,
authtypes.AuthHTTPPathAuthorize,
authtypes.AuthResponseType,
authtypes.AuthClientID,
url.QueryEscape(authtypes.AuthRedirect),
auth.State,
codeChallenge,
authtypes.AuthCodeChallengeMethod,
)
_ = services
// if len(services) > 0 {
// outURL = fmt.Sprintf("%s&scope=%s",
// outURL,
// strings.Join(services, ","),
// )
// }
return outURL, nil
}
func (s *service) AuthServiceCompleteFlow(ctx context.Context, request *protocoltypes.AuthServiceCompleteFlow_Request) (*protocoltypes.AuthServiceCompleteFlow_Reply, error) {
u, err := url.Parse(request.CallbackURL)
if err != nil {
return nil, err
}
if e := u.Query().Get("error"); e != "" {
return nil, errcode.ErrServicesAuthServer.Wrap(fmt.Errorf("got error: %s (%s)", e, u.Query().Get("error_description")))
}
code, state := u.Query().Get("code"), u.Query().Get("state")
authUntyped := s.authSession.Load()
if authUntyped == nil {
return nil, errcode.ErrServicesAuthNotInitialized
}
auth, ok := authUntyped.(*bertyauth.AuthSession)
if !ok {
return nil, errcode.ErrServicesAuthNotInitialized
}
if auth.State != state {
return nil, errcode.ErrServicesAuthWrongState
}
endpoint := fmt.Sprintf("%s%s", auth.BaseURL, authtypes.AuthHTTPPathTokenExchange)
s.logger.Debug("auth service start", logutil.PrivateString("endpoint", endpoint))
res, err := ctxhttp.PostForm(ctx, http.DefaultClient, endpoint, url.Values{
"grant_type": {authtypes.AuthGrantType},
"code": {code},
"client_id": {authtypes.AuthClientID},
"code_verifier": {auth.CodeVerifier},
})
if err != nil {
return nil, errcode.ErrStreamWrite.Wrap(err)
}
defer res.Body.Close()
if res.StatusCode >= 300 {
return nil, errcode.ErrServicesAuthInvalidResponse.Wrap(fmt.Errorf("invalid status code %d", res.StatusCode))
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, errcode.ErrStreamRead.Wrap(err)
}
resMsg := &protocoltypes.AuthExchangeResponse{}
if err := json.Unmarshal(body, &resMsg); err != nil {
return nil, errcode.ErrDeserialization.Wrap(err)
}
if resMsg.Error != "" {
return nil, errcode.ErrServicesAuthServer.Wrap(err)
}
if resMsg.AccessToken == "" {
return nil, errcode.ErrServicesAuthInvalidResponse.Wrap(fmt.Errorf("missing access token in response"))
}
if len(resMsg.Services) == 0 {
return nil, errcode.ErrServicesAuthInvalidResponse.Wrap(fmt.Errorf("no services returned along token"))
}
services := make([]*protocoltypes.ServiceTokenSupportedService, len(resMsg.Services))
i := 0
for k, v := range resMsg.Services {
services[i] = &protocoltypes.ServiceTokenSupportedService{
ServiceType: k,
ServiceEndpoint: v,
}
i++
}
s.logger.Debug("auth flow services", zap.Any("services", services))
svcToken := &protocoltypes.ServiceToken{
Token: resMsg.AccessToken,
AuthenticationURL: auth.BaseURL,
SupportedServices: services,
Expiration: -1,
}
if _, err := s.accountGroup.metadataStore.SendAccountServiceTokenAdded(ctx, svcToken); err != nil {
return nil, err
}
// @FIXME(gfanton): should be handle on the client (js) side
registeredPushServer := 0
for _, service := range services {
if service.ServiceType != authtypes.ServicePushID {
continue
}
s.logger.Debug("registering push server", logutil.PrivateString("endpoint", service.GetServiceEndpoint()))
client, err := s.getPushClient(service.ServiceEndpoint)
if err != nil {
s.logger.Warn("unable to connect to push server", logutil.PrivateString("endpoint", service.ServiceEndpoint), zap.Error(err))
continue
}
repl, err := client.ServerInfo(ctx, &pushtypes.PushServiceServerInfo_Request{})
s.logger.Debug("server info", zap.Int("supported push services ", len(repl.GetSupportedTokenTypes())))
if err != nil {
s.logger.Warn("unable to get server info from push server", logutil.PrivateString("endpoint", service.ServiceEndpoint), zap.Error(err))
continue
}
_, err = s.PushSetServer(ctx, &protocoltypes.PushSetServer_Request{
Server: &protocoltypes.PushServer{
ServerKey: repl.PublicKey,
ServiceAddr: service.ServiceEndpoint,
},
})
if err != nil {
s.logger.Warn("unable to set push server", zap.Error(err))
continue
}
registeredPushServer++
s.logger.Debug("push server registered", logutil.PrivateString("endpoint", service.GetServiceEndpoint()))
}
if registeredPushServer == 0 {
s.logger.Warn("no push server found/registered")
}
return &protocoltypes.AuthServiceCompleteFlow_Reply{
TokenID: svcToken.TokenID(),
}, nil
}
func (s *service) AuthServiceInitFlow(ctx context.Context, request *protocoltypes.AuthServiceInitFlow_Request) (*protocoltypes.AuthServiceInitFlow_Reply, error) {
u, err := s.authInitURL(request.AuthURL, request.Services...)
if err != nil {
return nil, err
}
return &protocoltypes.AuthServiceInitFlow_Reply{
URL: u,
SecureURL: strings.HasPrefix(u, "https://"),
}, nil
}
func (s *service) ServicesTokenList(request *protocoltypes.ServicesTokenList_Request, server protocoltypes.ProtocolService_ServicesTokenListServer) error {
for _, t := range s.accountGroup.metadataStore.listServiceTokens() {
if server.Context().Err() != nil {
break
}
if err := server.Send(&protocoltypes.ServicesTokenList_Reply{
TokenID: t.TokenID(),
Service: t,
}); err != nil {
return err
}
}
return nil
}
func (s *service) DebugAuthServiceSetToken(ctx context.Context, request *protocoltypes.DebugAuthServiceSetToken_Request) (*protocoltypes.DebugAuthServiceSetToken_Reply, error) {
services := make([]*protocoltypes.ServiceTokenSupportedService, len(request.Token.Services))
i := 0
for k, v := range request.Token.Services {
services[i] = &protocoltypes.ServiceTokenSupportedService{
ServiceType: k,
ServiceEndpoint: v,
}
i++
}
svcToken := &protocoltypes.ServiceToken{
Token: request.Token.AccessToken,
AuthenticationURL: request.AuthenticationURL,
SupportedServices: services,
Expiration: -1,
}
if _, err := s.accountGroup.metadataStore.SendAccountServiceTokenAdded(ctx, svcToken); err != nil {
return nil, err
}
return &protocoltypes.DebugAuthServiceSetToken_Reply{}, nil
}