auth.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package auth
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "github.com/gravitl/netmaker/logic"
  8. "github.com/gravitl/netmaker/models"
  9. "github.com/gravitl/netmaker/servercfg"
  10. "golang.org/x/crypto/bcrypt"
  11. "golang.org/x/oauth2"
  12. )
  13. // == consts ==
  14. const (
  15. init_provider = "initprovider"
  16. get_user_info = "getuserinfo"
  17. handle_callback = "handlecallback"
  18. handle_login = "handlelogin"
  19. google_provider_name = "google"
  20. azure_ad_provider_name = "azure-ad"
  21. github_provider_name = "github"
  22. verify_user = "verifyuser"
  23. auth_key = "netmaker_auth"
  24. )
  25. var oauth_state_string = "netmaker-oauth-state" // should be set randomly each provider login
  26. var auth_provider *oauth2.Config
  27. func getCurrentAuthFunctions() map[string]interface{} {
  28. var authInfo = servercfg.GetAuthProviderInfo()
  29. var authProvider = authInfo[0]
  30. switch authProvider {
  31. case google_provider_name:
  32. return google_functions
  33. case azure_ad_provider_name:
  34. return azure_ad_functions
  35. case github_provider_name:
  36. return github_functions
  37. default:
  38. return nil
  39. }
  40. }
  41. // InitializeAuthProvider - initializes the auth provider if any is present
  42. func InitializeAuthProvider() string {
  43. var functions = getCurrentAuthFunctions()
  44. if functions == nil {
  45. return ""
  46. }
  47. var _, err = fetchPassValue(logic.RandomString(64))
  48. if err != nil {
  49. logic.Log(err.Error(), 0)
  50. return ""
  51. }
  52. var currentFrontendURL = servercfg.GetFrontendURL()
  53. if currentFrontendURL == "" {
  54. return ""
  55. }
  56. var authInfo = servercfg.GetAuthProviderInfo()
  57. functions[init_provider].(func(string, string, string))(servercfg.GetAPIConnString()+"/api/oauth/callback", authInfo[1], authInfo[2])
  58. return authInfo[0]
  59. }
  60. // HandleAuthCallback - handles oauth callback
  61. func HandleAuthCallback(w http.ResponseWriter, r *http.Request) {
  62. if auth_provider == nil {
  63. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  64. fmt.Fprintln(w, oauthNotConfigured)
  65. return
  66. }
  67. var functions = getCurrentAuthFunctions()
  68. if functions == nil {
  69. return
  70. }
  71. functions[handle_callback].(func(http.ResponseWriter, *http.Request))(w, r)
  72. }
  73. // HandleAuthLogin - handles oauth login
  74. func HandleAuthLogin(w http.ResponseWriter, r *http.Request) {
  75. if auth_provider == nil {
  76. var referer = r.Header.Get("referer")
  77. if referer != "" {
  78. http.Redirect(w, r, referer+"?oauth=callback-error", http.StatusTemporaryRedirect)
  79. return
  80. }
  81. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  82. fmt.Fprintln(w, oauthNotConfigured)
  83. return
  84. }
  85. var functions = getCurrentAuthFunctions()
  86. if functions == nil {
  87. return
  88. }
  89. functions[handle_login].(func(http.ResponseWriter, *http.Request))(w, r)
  90. }
  91. // IsOauthUser - returns
  92. func IsOauthUser(user *models.User) error {
  93. var currentValue, err = fetchPassValue("")
  94. if err != nil {
  95. return err
  96. }
  97. var bCryptErr = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(currentValue))
  98. return bCryptErr
  99. }
  100. // == private methods ==
  101. func addUser(email string) error {
  102. var hasAdmin, err = logic.HasAdmin()
  103. if err != nil {
  104. logic.Log("error checking for existence of admin user during OAuth login for "+email+", user not added", 1)
  105. return err
  106. } // generate random password to adapt to current model
  107. var newPass, fetchErr = fetchPassValue("")
  108. if fetchErr != nil {
  109. return fetchErr
  110. }
  111. var newUser = models.User{
  112. UserName: email,
  113. Password: newPass,
  114. }
  115. if !hasAdmin { // must be first attempt, create an admin
  116. if newUser, err = logic.CreateAdmin(newUser); err != nil {
  117. logic.Log("error creating admin from user, "+email+", user not added", 1)
  118. } else {
  119. logic.Log("admin created from user, "+email+", was first user added", 0)
  120. }
  121. } else { // otherwise add to db as admin..?
  122. // TODO: add ability to add users with preemptive permissions
  123. newUser.IsAdmin = false
  124. if newUser, err = logic.CreateUser(newUser); err != nil {
  125. logic.Log("error creating user, "+email+", user not added", 1)
  126. } else {
  127. logic.Log("user created from, "+email+"", 0)
  128. }
  129. }
  130. return nil
  131. }
  132. func fetchPassValue(newValue string) (string, error) {
  133. type valueHolder struct {
  134. Value string `json:"value" bson:"value"`
  135. }
  136. var b64NewValue = base64.StdEncoding.EncodeToString([]byte(newValue))
  137. var newValueHolder = &valueHolder{
  138. Value: b64NewValue,
  139. }
  140. var data, marshalErr = json.Marshal(newValueHolder)
  141. if marshalErr != nil {
  142. return "", marshalErr
  143. }
  144. var currentValue, err = logic.FetchAuthSecret(auth_key, string(data))
  145. if err != nil {
  146. return "", err
  147. }
  148. var unmarshErr = json.Unmarshal([]byte(currentValue), newValueHolder)
  149. if unmarshErr != nil {
  150. return "", unmarshErr
  151. }
  152. var b64CurrentValue, b64Err = base64.StdEncoding.DecodeString(newValueHolder.Value)
  153. if b64Err != nil {
  154. logic.Log("could not decode pass", 0)
  155. return "", nil
  156. }
  157. return string(b64CurrentValue), nil
  158. }