loginHelper.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package login
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "io/ioutil"
  18. "math/rand"
  19. "net/http"
  20. "net/url"
  21. "strings"
  22. "github.com/pkg/browser"
  23. "github.com/pkg/errors"
  24. )
  25. type apiHelper interface {
  26. queryToken(data url.Values, tenantID string) (azureToken, error)
  27. openAzureLoginPage(redirectURL string) error
  28. queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error)
  29. }
  30. type azureAPIHelper struct{}
  31. func (helper azureAPIHelper) openAzureLoginPage(redirectURL string) error {
  32. state := randomString("", 10)
  33. authURL := fmt.Sprintf(authorizeFormat, clientID, redirectURL, state, scopes)
  34. return browser.OpenURL(authURL)
  35. }
  36. func (helper azureAPIHelper) queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error) {
  37. req, err := http.NewRequest(http.MethodGet, authorizationURL, nil)
  38. if err != nil {
  39. return nil, 0, err
  40. }
  41. req.Header.Add("Authorization", authorizationHeader)
  42. res, err := http.DefaultClient.Do(req)
  43. if err != nil {
  44. return nil, 0, err
  45. }
  46. bits, err := ioutil.ReadAll(res.Body)
  47. if err != nil {
  48. return nil, 0, err
  49. }
  50. return bits, res.StatusCode, nil
  51. }
  52. func (helper azureAPIHelper) queryToken(data url.Values, tenantID string) (azureToken, error) {
  53. res, err := http.Post(fmt.Sprintf(tokenEndpoint, tenantID), "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
  54. if err != nil {
  55. return azureToken{}, err
  56. }
  57. if res.StatusCode != 200 {
  58. return azureToken{}, errors.Errorf("error while renewing access token, status : %s", res.Status)
  59. }
  60. bits, err := ioutil.ReadAll(res.Body)
  61. if err != nil {
  62. return azureToken{}, err
  63. }
  64. token := azureToken{}
  65. if err := json.Unmarshal(bits, &token); err != nil {
  66. return azureToken{}, err
  67. }
  68. return token, nil
  69. }
  70. var (
  71. letterRunes = []rune("abcdefghijklmnopqrstuvwxyz123456789")
  72. )
  73. func randomString(prefix string, length int) string {
  74. b := make([]rune, length)
  75. for i := range b {
  76. b[i] = letterRunes[rand.Intn(len(letterRunes))]
  77. }
  78. return prefix + string(b)
  79. }