MCPcopy Create free account
hub / github.com/daodst/chat / VerifyUserFromRequest

Function VerifyUserFromRequest

clientapi/auth/auth.go:53–89  ·  view source on GitHub ↗

VerifyUserFromRequest authenticates the HTTP request, on success returns Device of the requester. Finds local user or an application service user. Note: For an AS user, AS dummy device is returned. On failure returns an JSON error response which can be sent to the client.

(
	req *http.Request, userAPI api.QueryAcccessTokenAPI,
)

Source from the content-addressed store, hash-verified

51// Note: For an AS user, AS dummy device is returned.
52// On failure returns an JSON error response which can be sent to the client.
53func VerifyUserFromRequest(
54 req *http.Request, userAPI api.QueryAcccessTokenAPI,
55) (*api.Device, *util.JSONResponse) {
56 // Try to find the Application Service user
57 token, err := ExtractAccessToken(req)
58 if err != nil {
59 return nil, &util.JSONResponse{
60 Code: http.StatusUnauthorized,
61 JSON: jsonerror.MissingToken(err.Error()),
62 }
63 }
64 var res api.QueryAccessTokenResponse
65 err = userAPI.QueryAccessToken(req.Context(), &api.QueryAccessTokenRequest{
66 AccessToken: token,
67 AppServiceUserID: req.URL.Query().Get("user_id"),
68 }, &res)
69 if err != nil {
70 util.GetLogger(req.Context()).WithError(err).Error("userAPI.QueryAccessToken failed")
71 jsonErr := jsonerror.InternalServerError()
72 return nil, &jsonErr
73 }
74 if res.Err != "" {
75 if strings.HasPrefix(strings.ToLower(res.Err), "forbidden:") { // TODO: use actual error and no string comparison
76 return nil, &util.JSONResponse{
77 Code: http.StatusForbidden,
78 JSON: jsonerror.Forbidden(res.Err),
79 }
80 }
81 }
82 if res.Device == nil {
83 return nil, &util.JSONResponse{
84 Code: http.StatusUnauthorized,
85 JSON: jsonerror.UnknownToken("Unknown token"),
86 }
87 }
88 return res.Device, nil
89}
90
91// GenerateAccessToken creates a new access token. Returns an error if failed to generate
92// random bytes.

Callers

nothing calls this directly

Calls 5

ExtractAccessTokenFunction · 0.85
ContextMethod · 0.80
QueryAccessTokenMethod · 0.65
GetMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected