authenticatedUserID returns the ID of the authenticated user, along with a bool value which indicates whether the user uses token authentication.
(store AuthStore, c *macaron.Context, sess session.Store)
| 141 | // authenticatedUserID returns the ID of the authenticated user, along with a bool value |
| 142 | // which indicates whether the user uses token authentication. |
| 143 | func authenticatedUserID(store AuthStore, c *macaron.Context, sess session.Store) (_ int64, isTokenAuth bool) { |
| 144 | if !database.HasEngine { |
| 145 | return 0, false |
| 146 | } |
| 147 | |
| 148 | // Check access token. |
| 149 | if isAPIPath(c.Req.URL.Path) { |
| 150 | var tokenSHA string |
| 151 | auHead := c.Req.Header.Get("Authorization") |
| 152 | if auHead != "" { |
| 153 | auths := strings.Fields(auHead) |
| 154 | if len(auths) == 2 && auths[0] == "token" { |
| 155 | tokenSHA = auths[1] |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Let's see if token is valid. |
| 160 | if len(tokenSHA) > 0 { |
| 161 | t, err := store.GetAccessTokenBySHA1(c.Req.Context(), tokenSHA) |
| 162 | if err != nil { |
| 163 | if !database.IsErrAccessTokenNotExist(err) { |
| 164 | log.Error("GetAccessTokenBySHA: %v", err) |
| 165 | } |
| 166 | return 0, false |
| 167 | } |
| 168 | if err = store.TouchAccessTokenByID(c.Req.Context(), t.ID); err != nil { |
| 169 | log.Error("Failed to touch access token: %v", err) |
| 170 | } |
| 171 | return t.UserID, true |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | uid := sess.Get("uid") |
| 176 | if uid == nil { |
| 177 | return 0, false |
| 178 | } |
| 179 | if id, ok := uid.(int64); ok { |
| 180 | _, err := store.GetUserByID(c.Req.Context(), id) |
| 181 | if err != nil { |
| 182 | if !database.IsErrUserNotExist(err) { |
| 183 | log.Error("Failed to get user by ID: %v", err) |
| 184 | } |
| 185 | return 0, false |
| 186 | } |
| 187 | return id, false |
| 188 | } |
| 189 | return 0, false |
| 190 | } |
| 191 | |
| 192 | // authenticatedUser returns the user object of the authenticated user, along with two bool values |
| 193 | // which indicate whether the user uses HTTP Basic Authentication or token authentication respectively. |
no test coverage detected