helper.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "os/exec"
  22. "runtime"
  23. "strings"
  24. "github.com/pkg/errors"
  25. )
  26. type apiHelper interface {
  27. queryToken(data url.Values, tenantID string) (azureToken, error)
  28. openAzureLoginPage(redirectURL string) error
  29. queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error)
  30. }
  31. type azureAPIHelper struct{}
  32. func (helper azureAPIHelper) openAzureLoginPage(redirectURL string) error {
  33. state := randomString("", 10)
  34. authURL := fmt.Sprintf(authorizeFormat, clientID, redirectURL, state, scopes)
  35. return openbrowser(authURL)
  36. }
  37. func (helper azureAPIHelper) queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error) {
  38. req, err := http.NewRequest(http.MethodGet, authorizationURL, nil)
  39. if err != nil {
  40. return nil, 0, err
  41. }
  42. req.Header.Add("Authorization", authorizationHeader)
  43. res, err := http.DefaultClient.Do(req)
  44. if err != nil {
  45. return nil, 0, err
  46. }
  47. bits, err := ioutil.ReadAll(res.Body)
  48. if err != nil {
  49. return nil, 0, err
  50. }
  51. return bits, res.StatusCode, nil
  52. }
  53. func (helper azureAPIHelper) queryToken(data url.Values, tenantID string) (azureToken, error) {
  54. res, err := http.Post(fmt.Sprintf(tokenEndpoint, tenantID), "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
  55. if err != nil {
  56. return azureToken{}, err
  57. }
  58. if res.StatusCode != 200 {
  59. return azureToken{}, errors.Errorf("error while renewing access token, status : %s", res.Status)
  60. }
  61. bits, err := ioutil.ReadAll(res.Body)
  62. if err != nil {
  63. return azureToken{}, err
  64. }
  65. token := azureToken{}
  66. if err := json.Unmarshal(bits, &token); err != nil {
  67. return azureToken{}, err
  68. }
  69. return token, nil
  70. }
  71. func openbrowser(url string) error {
  72. switch runtime.GOOS {
  73. case "linux":
  74. return exec.Command("xdg-open", url).Start()
  75. case "windows":
  76. return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
  77. case "darwin":
  78. return exec.Command("open", url).Start()
  79. default:
  80. return fmt.Errorf("unsupported platform")
  81. }
  82. }
  83. var (
  84. letterRunes = []rune("abcdefghijklmnopqrstuvwxyz123456789")
  85. )
  86. func randomString(prefix string, length int) string {
  87. b := make([]rune, length)
  88. for i := range b {
  89. b[i] = letterRunes[rand.Intn(len(letterRunes))]
  90. }
  91. return prefix + string(b)
  92. }