auth.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package auth
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "os"
  10. "strings"
  11. )
  12. // Where we store the config file
  13. const CONFIGFILE = "/var/lib/docker/.dockercfg"
  14. // the registry server we want to login against
  15. const REGISTRY_SERVER = "http://registry.docker.io"
  16. type AuthConfig struct {
  17. Username string `json:"username"`
  18. Password string `json:"password"`
  19. Email string `json:"email"`
  20. }
  21. // create a base64 encoded auth string to store in config
  22. func EncodeAuth(authConfig AuthConfig) string {
  23. authStr := authConfig.Username + ":" + authConfig.Password
  24. msg := []byte(authStr)
  25. encoded := make([]byte, base64.StdEncoding.EncodedLen(len(msg)))
  26. base64.StdEncoding.Encode(encoded, msg)
  27. return string(encoded)
  28. }
  29. // decode the auth string
  30. func DecodeAuth(authStr string) (AuthConfig, error) {
  31. decLen := base64.StdEncoding.DecodedLen(len(authStr))
  32. decoded := make([]byte, decLen)
  33. authByte := []byte(authStr)
  34. n, err := base64.StdEncoding.Decode(decoded, authByte)
  35. if err != nil {
  36. return AuthConfig{}, err
  37. }
  38. if n > decLen {
  39. return AuthConfig{}, errors.New("something went wrong decoding auth config")
  40. }
  41. arr := strings.Split(string(decoded), ":")
  42. password := strings.Trim(arr[1], "\x00")
  43. return AuthConfig{Username: arr[0], Password: password}, nil
  44. }
  45. // load up the auth config information and return values
  46. func LoadConfig() (AuthConfig, error) {
  47. if _, err := os.Stat(CONFIGFILE); err == nil {
  48. b, err := ioutil.ReadFile(CONFIGFILE)
  49. if err != nil {
  50. return AuthConfig{}, err
  51. }
  52. arr := strings.Split(string(b), "\n")
  53. orig_auth := strings.Split(arr[0], " = ")
  54. orig_email := strings.Split(arr[1], " = ")
  55. authConfig, err := DecodeAuth(orig_auth[1])
  56. if err != nil {
  57. return AuthConfig{}, err
  58. }
  59. authConfig.Email = orig_email[1]
  60. return authConfig, nil
  61. } else {
  62. return AuthConfig{}, nil
  63. }
  64. return AuthConfig{}, nil
  65. }
  66. // save the auth config
  67. func saveConfig(authStr string, email string) error {
  68. lines := "auth = " + authStr + "\n" + "email = " + email + "\n"
  69. b := []byte(lines)
  70. err := ioutil.WriteFile(CONFIGFILE, b, 0600)
  71. if err != nil {
  72. return err
  73. }
  74. return nil
  75. }
  76. // try to register/login to the registry server
  77. func Login(authConfig AuthConfig) (string, error) {
  78. storeConfig := false
  79. reqStatusCode := 0
  80. var status string
  81. var errMsg string
  82. var reqBody []byte
  83. jsonBody, err := json.Marshal(authConfig)
  84. if err != nil {
  85. errMsg = fmt.Sprintf("Config Error: %s", err)
  86. return "", errors.New(errMsg)
  87. }
  88. b := strings.NewReader(string(jsonBody))
  89. req1, err := http.Post(REGISTRY_SERVER+"/v1/users", "application/json; charset=utf-8", b)
  90. if err != nil {
  91. errMsg = fmt.Sprintf("Server Error: %s", err)
  92. return "", errors.New(errMsg)
  93. }
  94. reqStatusCode = req1.StatusCode
  95. defer req1.Body.Close()
  96. reqBody, err = ioutil.ReadAll(req1.Body)
  97. if err != nil {
  98. errMsg = fmt.Sprintf("Server Error: [%#v] %s", reqStatusCode, err)
  99. return "", errors.New(errMsg)
  100. }
  101. if reqStatusCode == 201 {
  102. status = "Account Created\n"
  103. storeConfig = true
  104. } else if reqStatusCode == 400 {
  105. if string(reqBody) == "Username or email already exist" {
  106. client := &http.Client{}
  107. req, err := http.NewRequest("GET", REGISTRY_SERVER+"/v1/users", nil)
  108. req.SetBasicAuth(authConfig.Username, authConfig.Password)
  109. resp, err := client.Do(req)
  110. if err != nil {
  111. return "", err
  112. }
  113. defer resp.Body.Close()
  114. body, err := ioutil.ReadAll(resp.Body)
  115. if err != nil {
  116. return "", err
  117. }
  118. if resp.StatusCode == 200 {
  119. status = "Login Succeeded\n"
  120. storeConfig = true
  121. } else {
  122. status = fmt.Sprintf("Login: %s", body)
  123. return "", errors.New(status)
  124. }
  125. } else {
  126. status = fmt.Sprintf("Registration: %s", string(reqBody))
  127. return "", errors.New(status)
  128. }
  129. } else {
  130. status = fmt.Sprintf("[%s] : %s", reqStatusCode, string(reqBody))
  131. return "", errors.New(status)
  132. }
  133. if storeConfig {
  134. authStr := EncodeAuth(authConfig)
  135. saveConfig(authStr, authConfig.Email)
  136. }
  137. return status, nil
  138. }