auth.go 4.3 KB

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