auth/internal/user/delivery/http/user_http.go

107 lines
2.5 KiB
Go
Raw Normal View History

2022-02-17 16:16:15 +00:00
package http
import (
"encoding/json"
"strings"
"github.com/fasthttp/router"
"github.com/lestrrat-go/jwx/jwa"
http "github.com/valyala/fasthttp"
"source.toby3d.me/toby3d/middleware"
"source.toby3d.me/toby3d/auth/internal/common"
"source.toby3d.me/toby3d/auth/internal/domain"
"source.toby3d.me/toby3d/auth/internal/token"
2022-02-17 16:16:15 +00:00
)
type (
UserInformationResponse struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Photo string `json:"photo,omitempty"`
Email string `json:"email,omitempty"`
2022-02-17 16:16:15 +00:00
}
RequestHandler struct {
config *domain.Config
tokens token.UseCase
}
)
func NewRequestHandler(tokens token.UseCase, config *domain.Config) *RequestHandler {
return &RequestHandler{
tokens: tokens,
config: config,
}
}
func (h *RequestHandler) Register(r *router.Router) {
chain := middleware.Chain{
middleware.JWTWithConfig(middleware.JWTConfig{
2022-02-25 23:18:04 +00:00
AuthScheme: "Bearer",
ContextKey: "token",
SigningKey: []byte(h.config.JWT.Secret),
SigningMethod: jwa.SignatureAlgorithm(h.config.JWT.Algorithm),
Skipper: middleware.DefaultSkipper,
TokenLookup: "header:" + http.HeaderAuthorization + ":Bearer ",
2022-02-17 16:16:15 +00:00
}),
middleware.LogFmt(),
}
r.GET("/userinfo", chain.RequestHandler(h.handleUserInformation))
}
func (h *RequestHandler) handleUserInformation(ctx *http.RequestCtx) {
ctx.SetContentType(common.MIMEApplicationJSONCharsetUTF8)
ctx.SetStatusCode(http.StatusOK)
encoder := json.NewEncoder(ctx)
2022-02-25 15:34:20 +00:00
tkn, userInfo, err := h.tokens.Verify(ctx, strings.TrimPrefix(string(ctx.Request.Header.Peek(
http.HeaderAuthorization)), "Bearer "))
2022-02-17 16:16:15 +00:00
if err != nil || tkn == nil {
// WARN(toby3d): If the token is not valid, the endpoint still
// MUST return a 200 Response.
_ = encoder.Encode(err)
return
}
if !tkn.Scope.Has(domain.ScopeProfile) {
2022-02-17 16:16:15 +00:00
ctx.SetStatusCode(http.StatusForbidden)
2022-02-17 16:16:15 +00:00
_ = encoder.Encode(domain.NewError(
domain.ErrorCodeInsufficientScope,
"token with 'profile' scope is required to view profile data",
2022-02-17 16:16:15 +00:00
"https://indieauth.net/source/#user-information",
))
return
}
resp := new(UserInformationResponse)
2022-02-25 15:34:20 +00:00
if userInfo == nil {
_ = encoder.Encode(resp)
return
}
2022-02-25 15:34:20 +00:00
if userInfo.HasName() {
resp.Name = userInfo.GetName()
}
2022-02-25 15:34:20 +00:00
if userInfo.HasURL() {
resp.URL = userInfo.GetURL().String()
}
2022-02-25 15:34:20 +00:00
if userInfo.HasPhoto() {
resp.Photo = userInfo.GetPhoto().String()
}
2022-02-25 15:34:20 +00:00
if tkn.Scope.Has(domain.ScopeEmail) && userInfo.HasEmail() {
resp.Email = userInfo.GetEmail().String()
}
_ = encoder.Encode(resp)
2022-02-17 16:16:15 +00:00
}