forked from SkygearIO/skygear-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
me.go
116 lines (103 loc) · 3.34 KB
/
me.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2015-present Oursky Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package handler
import (
"github.com/skygeario/skygear-server/pkg/server/asset"
"github.com/skygeario/skygear-server/pkg/server/authtoken"
"github.com/skygeario/skygear-server/pkg/server/router"
"github.com/skygeario/skygear-server/pkg/server/skyerr"
)
// MeHandler handles the me request
type MeHandler struct {
TokenStore authtoken.Store `inject:"TokenStore"`
AssetStore asset.Store `inject:"AssetStore"`
Authenticator router.Processor `preprocessor:"authenticator"`
DBConn router.Processor `preprocessor:"dbconn"`
InjectAuth router.Processor `preprocessor:"require_auth"`
InjectUser router.Processor `preprocessor:"require_user"`
InjectPublicDB router.Processor `preprocessor:"inject_public_db"`
PluginReady router.Processor `preprocessor:"plugin_ready"`
preprocessors []router.Processor
}
// Setup adds injected pre-processors to preprocessors array
func (h *MeHandler) Setup() {
h.preprocessors = []router.Processor{
h.Authenticator,
h.DBConn,
h.InjectAuth,
h.InjectUser,
h.InjectPublicDB,
h.PluginReady,
}
}
// GetPreprocessors returns all pre-processors for the handler
func (h *MeHandler) GetPreprocessors() []router.Processor {
return h.preprocessors
}
// Handle is the handling method of the me request
// curl -X POST -H "Content-Type: application/json" \
// -d @- http://localhost:3000/ <<EOF
// {
// "action": "me"
// }
// EOF
//
// {
// "user_id": "3df4b52b-bd58-4fa2-8aee-3d44fd7f974d",
// "username": "user1",
// "last_login_at": "2016-09-08T06:42:59.871181Z",
// "last_seen_at": "2016-09-08T07:15:18.026567355Z",
// "roles": []
// }
func (h *MeHandler) Handle(payload *router.Payload, response *router.Response) {
info := payload.AuthInfo
if info == nil {
response.Err = skyerr.NewError(skyerr.NotAuthenticated, "Authentication is needed to get current user")
return
}
if h.TokenStore == nil {
panic("token store is nil")
}
store := h.TokenStore
// refresh access token with a newly generated one
token, err := store.NewToken(payload.AppName, info.ID)
if err != nil {
panic(err)
}
if err = store.Put(&token); err != nil {
panic(err)
}
user := payload.User
if user == nil {
panic("user record not found")
}
// We will return the last seen in DB, not current time stamp
authResponse, err := AuthResponseFactory{
AssetStore: h.AssetStore,
Conn: payload.DBConn,
Database: payload.Database,
}.NewAuthResponse(*info, *user, token.AccessToken, payload.HasMasterKey())
if err != nil {
response.Err = skyerr.MakeError(err)
return
}
// Populate the activity time to user
now := timeNow()
info.LastSeenAt = &now
if err := payload.DBConn.UpdateAuth(info); err != nil {
response.Err = skyerr.MakeError(err)
return
}
response.Result = authResponse
}