summaryrefslogtreecommitdiffstats
path: root/api/user.go
diff options
context:
space:
mode:
author=Corey Hulen <corey@hulen.com>2016-01-26 20:32:24 -0500
committer=Corey Hulen <corey@hulen.com>2016-01-26 20:32:24 -0500
commitc2bc9454ce5550a180d8ee9fec75db7f3841b1ad (patch)
treebb56d39a6eb468dc002486723bdb085ca7b20a3b /api/user.go
parent21d51f8c9094f434afed8d23d4790aea28a4c0df (diff)
downloadchat-c2bc9454ce5550a180d8ee9fec75db7f3841b1ad.tar.gz
chat-c2bc9454ce5550a180d8ee9fec75db7f3841b1ad.tar.bz2
chat-c2bc9454ce5550a180d8ee9fec75db7f3841b1ad.zip
PLT-1586 adding attach device id method
Diffstat (limited to 'api/user.go')
-rw-r--r--api/user.go45
1 files changed, 44 insertions, 1 deletions
diff --git a/api/user.go b/api/user.go
index 473f0da54..ceaf1fc2d 100644
--- a/api/user.go
+++ b/api/user.go
@@ -48,6 +48,7 @@ func InitUser(r *mux.Router) {
sr.Handle("/logout", ApiUserRequired(logout)).Methods("POST")
sr.Handle("/login_ldap", ApiAppHandler(loginLdap)).Methods("POST")
sr.Handle("/revoke_session", ApiUserRequired(revokeSession)).Methods("POST")
+ sr.Handle("/attach_device", ApiUserRequired(attachDeviceId)).Methods("POST")
sr.Handle("/switch_to_sso", ApiAppHandler(switchToSSO)).Methods("POST")
sr.Handle("/switch_to_email", ApiUserRequired(switchToEmail)).Methods("POST")
@@ -546,7 +547,6 @@ func Login(c *Context, w http.ResponseWriter, r *http.Request, user *model.User,
}
}
}
-
} else {
session.SetExpireInDays(*utils.Cfg.ServiceSettings.SessionLengthWebInDays)
}
@@ -718,6 +718,49 @@ func revokeSession(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(model.MapToJson(props)))
}
+func attachDeviceId(c *Context, w http.ResponseWriter, r *http.Request) {
+ props := model.MapFromJson(r.Body)
+
+ deviceId := props["device_id"]
+ if len(deviceId) == 0 {
+ c.SetInvalidParam("attachDevice", "deviceId")
+ return
+ }
+
+ if !(strings.HasPrefix(deviceId, model.PUSH_NOTIFY_APPLE+":") || strings.HasPrefix(deviceId, model.PUSH_NOTIFY_ANDROID+":")) {
+ c.SetInvalidParam("attachDevice", "deviceId")
+ return
+ }
+
+ // A special case where we logout of all other sessions with the same Id
+ if result := <-Srv.Store.Session().GetSessions(c.Session.UserId); result.Err != nil {
+ c.Err = result.Err
+ c.Err.StatusCode = http.StatusForbidden
+ return
+ } else {
+ sessions := result.Data.([]*model.Session)
+ for _, session := range sessions {
+ if session.DeviceId == deviceId && session.Id != c.Session.Id {
+ l4g.Debug(utils.T("api.user.login.revoking.app_error"), session.Id, c.Session.UserId)
+ RevokeSessionById(c, session.Id)
+ if c.Err != nil {
+ c.LogError(c.Err)
+ c.Err = nil
+ }
+ }
+ }
+ }
+
+ sessionCache.Remove(c.Session.Token)
+
+ if result := <-Srv.Store.Session().UpdateDeviceId(c.Session.Id, deviceId); result.Err != nil {
+ c.Err = result.Err
+ return
+ }
+
+ w.Write([]byte(deviceId))
+}
+
func RevokeSessionById(c *Context, sessionId string) {
if result := <-Srv.Store.Session().Get(sessionId); result.Err != nil {
c.Err = result.Err