MCPcopy Create free account
hub / github.com/gogs/gogs / authenticatedUserID

Function authenticatedUserID

internal/context/auth.go:143–190  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

141// authenticatedUserID returns the ID of the authenticated user, along with a bool value
142// which indicates whether the user uses token authentication.
143func 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.

Callers 1

authenticatedUserFunction · 0.85

Calls 8

IsErrAccessTokenNotExistFunction · 0.92
IsErrUserNotExistFunction · 0.92
isAPIPathFunction · 0.85
GetMethod · 0.65
GetAccessTokenBySHA1Method · 0.65
TouchAccessTokenByIDMethod · 0.65
GetUserByIDMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected