auth.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 reqBody []byte
  82. jsonBody, _ := json.Marshal(authConfig)
  83. b := strings.NewReader(string(jsonBody))
  84. req1, err := http.Post(REGISTRY_SERVER+"/v1/users", "application/json; charset=utf-8", b)
  85. if err == nil {
  86. body, _ := ioutil.ReadAll(req1.Body)
  87. reqStatusCode = req1.StatusCode
  88. reqBody = body
  89. req1.Body.Close()
  90. } else {
  91. return "", err
  92. }
  93. if reqStatusCode == 201 {
  94. status = "Account Created\n"
  95. storeConfig = true
  96. } else if reqStatusCode == 400 {
  97. if string(reqBody) == "Username or email already exist" {
  98. client := &http.Client{}
  99. req, err := http.NewRequest("GET", REGISTRY_SERVER+"/v1/users", nil)
  100. req.SetBasicAuth(authConfig.Username, authConfig.Password)
  101. resp, err := client.Do(req)
  102. if err != nil {
  103. return "", err
  104. }
  105. defer resp.Body.Close()
  106. body, err := ioutil.ReadAll(resp.Body)
  107. if err != nil {
  108. return "", err
  109. }
  110. if resp.StatusCode == 200 {
  111. status = "Login Succeeded\n"
  112. storeConfig = true
  113. } else {
  114. storeConfig = false
  115. status = fmt.Sprintf("Error: %s\n", body)
  116. }
  117. } else {
  118. status = fmt.Sprintf("Error: %s\n", reqBody)
  119. }
  120. }
  121. if storeConfig {
  122. authStr := EncodeAuth(authConfig)
  123. saveConfig(authStr, authConfig.Email)
  124. }
  125. return status, nil
  126. }