local_server.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "fmt"
  16. "net"
  17. "net/http"
  18. "net/url"
  19. "github.com/pkg/errors"
  20. )
  21. const failHTML = `
  22. <!DOCTYPE html>
  23. <html>
  24. <head>
  25. <meta charset="utf-8" />
  26. <title>Login failed</title>
  27. </head>
  28. <body>
  29. <h4>Some failures occurred during the authentication</h4>
  30. <p>You can log an issue at <a href="https://github.com/azure/azure-cli/issues">Azure CLI GitHub Repository</a> and we will assist you in resolving it.</p>
  31. </body>
  32. </html>
  33. `
  34. const successHTML = `
  35. <!DOCTYPE html>
  36. <html>
  37. <head>
  38. <meta charset="utf-8" />
  39. <meta http-equiv="refresh" content="10;url=https://docs.microsoft.com/cli/azure/">
  40. <title>Login successfully</title>
  41. </head>
  42. <body>
  43. <h4>You have logged into Microsoft Azure!</h4>
  44. <p>You can close this window, or we will redirect you to the <a href="https://docs.microsoft.com/cli/azure/">Azure CLI documents</a> in 10 seconds.</p>
  45. </body>
  46. </html>
  47. `
  48. const (
  49. // redirectHost is where the user's browser is redirected on authentication
  50. // completion. Note that only registered hosts can be used. i.e.:
  51. // "localhost" works but "127.0.0.1" does not.
  52. redirectHost = "localhost"
  53. )
  54. type localResponse struct {
  55. values url.Values
  56. err error
  57. }
  58. // LocalServer is an Azure login server
  59. type LocalServer struct {
  60. httpServer *http.Server
  61. listener net.Listener
  62. queryCh chan localResponse
  63. }
  64. // NewLocalServer creates an Azure login server
  65. func NewLocalServer(queryCh chan localResponse) (*LocalServer, error) {
  66. s := &LocalServer{queryCh: queryCh}
  67. mux := http.NewServeMux()
  68. mux.HandleFunc("/", s.handler())
  69. s.httpServer = &http.Server{Handler: mux}
  70. l, err := net.Listen("tcp", redirectHost+":0")
  71. if err != nil {
  72. return nil, err
  73. }
  74. s.listener = l
  75. p := l.Addr().(*net.TCPAddr).Port
  76. if p == 0 {
  77. return nil, errors.New("unable to allocate login server port")
  78. }
  79. return s, nil
  80. }
  81. // Serve starts the local Azure login server
  82. func (s *LocalServer) Serve() {
  83. go func() {
  84. if err := s.httpServer.Serve(s.listener); err != nil {
  85. s.queryCh <- localResponse{
  86. err: errors.Wrap(err, "unable to start login server"),
  87. }
  88. }
  89. }()
  90. }
  91. // Close stops the local Azure login server
  92. func (s *LocalServer) Close() {
  93. _ = s.httpServer.Close()
  94. }
  95. // Addr returns the address that the local Azure server is service to
  96. func (s *LocalServer) Addr() string {
  97. return fmt.Sprintf("http://%s:%d", redirectHost, s.listener.Addr().(*net.TCPAddr).Port)
  98. }
  99. func (s *LocalServer) handler() func(w http.ResponseWriter, r *http.Request) {
  100. return func(w http.ResponseWriter, r *http.Request) {
  101. if _, hasCode := r.URL.Query()["code"]; hasCode {
  102. if _, err := w.Write([]byte(successHTML)); err != nil {
  103. s.queryCh <- localResponse{
  104. err: errors.Wrap(err, "unable to write success HTML"),
  105. }
  106. } else {
  107. s.queryCh <- localResponse{values: r.URL.Query()}
  108. }
  109. } else {
  110. if _, err := w.Write([]byte(failHTML)); err != nil {
  111. s.queryCh <- localResponse{
  112. err: errors.Wrap(err, "unable to write fail HTML"),
  113. }
  114. } else {
  115. s.queryCh <- localResponse{values: r.URL.Query()}
  116. }
  117. }
  118. }
  119. }