diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-04-10 08:19:49 -0400 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2017-04-10 08:19:49 -0400 |
commit | dfc6db737411bd4ad68a803be5182f06055a1769 (patch) | |
tree | 375d93b8d1d8b1384988dc708c9f337e0ea2366c /api4/user.go | |
parent | 7b77bcf87e85330a1f7f0b2a2dcbf71326bf2fba (diff) | |
download | chat-dfc6db737411bd4ad68a803be5182f06055a1769.tar.gz chat-dfc6db737411bd4ad68a803be5182f06055a1769.tar.bz2 chat-dfc6db737411bd4ad68a803be5182f06055a1769.zip |
Refactor switching login type code into app layer and add v4 endpoint (#6000)
* Refactor switching login type code into app layer and add v4 endpoint
* Fix unit test
Diffstat (limited to 'api4/user.go')
-rw-r--r-- | api4/user.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/api4/user.go b/api4/user.go index b30d066ab..5880370c6 100644 --- a/api4/user.go +++ b/api4/user.go @@ -43,6 +43,7 @@ func InitUser() { BaseRoutes.User.Handle("/mfa/generate", ApiSessionRequired(generateMfaSecret)).Methods("POST") BaseRoutes.Users.Handle("/login", ApiHandler(login)).Methods("POST") + BaseRoutes.Users.Handle("/login/switch", ApiHandler(switchAccountType)).Methods("POST") BaseRoutes.Users.Handle("/logout", ApiHandler(logout)).Methods("POST") BaseRoutes.UserByUsername.Handle("", ApiSessionRequired(getUserByUsername)).Methods("GET") @@ -981,3 +982,40 @@ func sendVerificationEmail(c *Context, w http.ResponseWriter, r *http.Request) { ReturnStatusOK(w) } + +func switchAccountType(c *Context, w http.ResponseWriter, r *http.Request) { + switchRequest := model.SwitchRequestFromJson(r.Body) + if switchRequest == nil { + c.SetInvalidParam("switch_request") + return + } + + link := "" + var err *model.AppError + + if switchRequest.EmailToOAuth() { + link, err = app.SwitchEmailToOAuth(switchRequest.Email, switchRequest.Password, switchRequest.MfaCode, switchRequest.NewService) + } else if switchRequest.OAuthToEmail() { + c.SessionRequired() + if c.Err != nil { + return + } + + link, err = app.SwitchOAuthToEmail(switchRequest.Email, switchRequest.NewPassword, c.Session.UserId) + } else if switchRequest.EmailToLdap() { + link, err = app.SwitchEmailToLdap(switchRequest.Email, switchRequest.Password, switchRequest.MfaCode, switchRequest.LdapId, switchRequest.NewPassword) + } else if switchRequest.LdapToEmail() { + link, err = app.SwitchLdapToEmail(switchRequest.Password, switchRequest.MfaCode, switchRequest.Email, switchRequest.NewPassword) + } else { + c.SetInvalidParam("switch_request") + return + } + + if err != nil { + c.Err = err + return + } + + c.LogAudit("success") + w.Write([]byte(model.MapToJson(map[string]string{"follow_link": link}))) +} |