| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package googleoauth | 5 package googleoauth |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/json" | 8 "encoding/json" |
| 9 "net/http" | 9 "net/http" |
| 10 "net/url" | 10 "net/url" |
| 11 | 11 |
| 12 "github.com/luci/luci-go/common/errors" | 12 "github.com/luci/luci-go/common/errors" |
| 13 "github.com/luci/luci-go/common/logging" | 13 "github.com/luci/luci-go/common/logging" |
| 14 "github.com/luci/luci-go/common/retry" |
| 14 "google.golang.org/api/googleapi" | 15 "google.golang.org/api/googleapi" |
| 15 | 16 |
| 16 "golang.org/x/net/context" | 17 "golang.org/x/net/context" |
| 17 "golang.org/x/net/context/ctxhttp" | 18 "golang.org/x/net/context/ctxhttp" |
| 18 ) | 19 ) |
| 19 | 20 |
| 20 const ( | 21 const ( |
| 21 // TokeninfoEndpoint is Google's token info endpoint. | 22 // TokeninfoEndpoint is Google's token info endpoint. |
| 22 TokeninfoEndpoint = "https://www.googleapis.com/oauth2/v3/tokeninfo" | 23 TokeninfoEndpoint = "https://www.googleapis.com/oauth2/v3/tokeninfo" |
| 23 ) | 24 ) |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 logging.Debugf(c, "POST %s", params.Endpoint) | 73 logging.Debugf(c, "POST %s", params.Endpoint) |
| 73 v := url.Values{} | 74 v := url.Values{} |
| 74 if params.IDToken != "" { | 75 if params.IDToken != "" { |
| 75 v.Add("id_token", params.IDToken) | 76 v.Add("id_token", params.IDToken) |
| 76 } else { | 77 } else { |
| 77 v.Add("access_token", params.AccessToken) | 78 v.Add("access_token", params.AccessToken) |
| 78 } | 79 } |
| 79 resp, err := ctxhttp.Get(c, params.Client, params.Endpoint+"?"+v.Encode(
)) | 80 resp, err := ctxhttp.Get(c, params.Client, params.Endpoint+"?"+v.Encode(
)) |
| 80 if err != nil { | 81 if err != nil { |
| 81 logging.WithError(err).Errorf(c, "POST %s failed", params.Endpoi
nt) | 82 logging.WithError(err).Errorf(c, "POST %s failed", params.Endpoi
nt) |
| 82 » » return nil, errors.WrapTransient(err) | 83 » » return nil, retry.Tag.Apply(err) |
| 83 } | 84 } |
| 84 defer googleapi.CloseBody(resp) | 85 defer googleapi.CloseBody(resp) |
| 85 if err := googleapi.CheckResponse(resp); err != nil { | 86 if err := googleapi.CheckResponse(resp); err != nil { |
| 86 logging.WithError(err).Errorf(c, "POST %s failed", params.Endpoi
nt) | 87 logging.WithError(err).Errorf(c, "POST %s failed", params.Endpoi
nt) |
| 87 if apiErr, ok := err.(*googleapi.Error); ok && apiErr.Code < 500
{ | 88 if apiErr, ok := err.(*googleapi.Error); ok && apiErr.Code < 500
{ |
| 88 return nil, ErrBadToken | 89 return nil, ErrBadToken |
| 89 } | 90 } |
| 90 » » return nil, errors.WrapTransient(err) | 91 » » return nil, retry.Tag.Apply(err) |
| 91 } | 92 } |
| 92 | 93 |
| 93 info := &TokenInfo{} | 94 info := &TokenInfo{} |
| 94 if err := json.NewDecoder(resp.Body).Decode(info); err != nil { | 95 if err := json.NewDecoder(resp.Body).Decode(info); err != nil { |
| 95 // This should never happen. If it does, the token endpoint has
gone mad, | 96 // This should never happen. If it does, the token endpoint has
gone mad, |
| 96 // and maybe it will recover soon. So mark the error as transien
t. | 97 // and maybe it will recover soon. So mark the error as transien
t. |
| 97 logging.WithError(err).Errorf(c, "Bad token info endpoint respon
se") | 98 logging.WithError(err).Errorf(c, "Bad token info endpoint respon
se") |
| 98 » » return nil, errors.WrapTransient(err) | 99 » » return nil, retry.Tag.Apply(err) |
| 99 } | 100 } |
| 100 | 101 |
| 101 return info, nil | 102 return info, nil |
| 102 } | 103 } |
| OLD | NEW |