Compare commits
3 Commits
040c662826
...
d1c5bde5c1
Author | SHA1 | Date |
---|---|---|
|
d1c5bde5c1 | |
|
26a6bda053 | |
|
93105fe5af |
|
@ -287,9 +287,11 @@ func (a AccountAPI) passwordResetConfirm(jc jape.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a AccountAPI) ping(jc jape.Context) {
|
func (a AccountAPI) ping(jc jape.Context) {
|
||||||
|
token := middleware.GetAuthTokenFromContext(jc.Request.Context())
|
||||||
account.EchoAuthCookie(jc, a.Name())
|
account.EchoAuthCookie(jc, a.Name())
|
||||||
jc.Encode(&PongResponse{
|
jc.Encode(&PongResponse{
|
||||||
Ping: "pong",
|
Ping: "pong",
|
||||||
|
Token: token,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,8 @@ type PasswordResetVerifyRequest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type PongResponse struct {
|
type PongResponse struct {
|
||||||
Ping string `json:"ping"`
|
Ping string `json:"ping"`
|
||||||
|
Token string `json:"token"`
|
||||||
}
|
}
|
||||||
type AccountInfoResponse struct {
|
type AccountInfoResponse struct {
|
||||||
ID uint `json:"id"`
|
ID uint `json:"id"`
|
||||||
|
|
|
@ -253,6 +253,8 @@ components:
|
||||||
properties:
|
properties:
|
||||||
ping:
|
ping:
|
||||||
type: string
|
type: string
|
||||||
|
token:
|
||||||
|
type: string
|
||||||
AccountInfoResponse:
|
AccountInfoResponse:
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
|
|
|
@ -16,6 +16,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const DEFAULT_AUTH_CONTEXT_KEY = "user_id"
|
const DEFAULT_AUTH_CONTEXT_KEY = "user_id"
|
||||||
|
const AUTH_TOKEN_CONTEXT_KEY = "auth_token"
|
||||||
|
|
||||||
type JapeMiddlewareFunc func(jape.Handler) jape.Handler
|
type JapeMiddlewareFunc func(jape.Handler) jape.Handler
|
||||||
type HttpMiddlewareFunc func(http.Handler) http.Handler
|
type HttpMiddlewareFunc func(http.Handler) http.Handler
|
||||||
|
@ -154,6 +155,7 @@ func AuthMiddleware(options AuthMiddlewareOptions) func(http.Handler) http.Handl
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.WithValue(r.Context(), options.AuthContextKey, uint(userId))
|
ctx := context.WithValue(r.Context(), options.AuthContextKey, uint(userId))
|
||||||
|
ctx = context.WithValue(r.Context(), AUTH_TOKEN_CONTEXT_KEY, authToken)
|
||||||
r = r.WithContext(ctx)
|
r = r.WithContext(ctx)
|
||||||
|
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
@ -192,6 +194,17 @@ func GetUserFromContext(ctx context.Context, key ...string) uint {
|
||||||
|
|
||||||
return userId
|
return userId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetAuthTokenFromContext(ctx context.Context) string {
|
||||||
|
authToken, ok := ctx.Value(AUTH_TOKEN_CONTEXT_KEY).(string)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
panic("auth token stored in context is not of type string")
|
||||||
|
}
|
||||||
|
|
||||||
|
return authToken
|
||||||
|
}
|
||||||
|
|
||||||
func CtxAborted(ctx context.Context) bool {
|
func CtxAborted(ctx context.Context) bool {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
|
Loading…
Reference in New Issue