♻️ Refactored user HTTP delivery schema

This commit is contained in:
Maxim Lebedev 2023-01-17 01:01:22 +06:00
parent e7f86d5de9
commit bf3499084e
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
4 changed files with 49 additions and 40 deletions

View File

@ -154,11 +154,8 @@ func (h *Handler) handleAuthorize(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
web.WriteTemplate(w, &web.ErrorPage{ web.WriteTemplate(w, &web.ErrorPage{
BaseOf: baseOf, BaseOf: baseOf,
Error: domain.NewError( Error: domain.NewError(domain.ErrorCodeInvalidClient, "requested redirect_uri is not"+
domain.ErrorCodeInvalidClient, " registered on client_id side", ""),
"requested redirect_uri is not registered on client_id side",
"",
),
}) })
return return

View File

@ -58,6 +58,7 @@ func (h *Handler) handleFunc(w http.ResponseWriter, r *http.Request) {
// WARN(toby3d): If the token is not valid, the endpoint still // WARN(toby3d): If the token is not valid, the endpoint still
// MUST return a 200 Response. // MUST return a 200 Response.
_ = encoder.Encode(err) //nolint:errchkjson _ = encoder.Encode(err) //nolint:errchkjson
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
return return
@ -70,34 +71,15 @@ func (h *Handler) handleFunc(w http.ResponseWriter, r *http.Request) {
"token with 'profile' scope is required to view profile data", "token with 'profile' scope is required to view profile data",
"https://indieauth.net/source/#user-information", "https://indieauth.net/source/#user-information",
)) ))
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
return return
} }
resp := new(UserInformationResponse) //nolint:errchkjson
if userInfo == nil { _ = encoder.Encode(NewUserInformationResponse(userInfo,
_ = encoder.Encode(resp) //nolint:errchkjson userInfo.HasEmail() && tkn.Scope.Has(domain.ScopeEmail)))
return
}
if userInfo.HasName() {
resp.Name = userInfo.GetName()
}
if userInfo.HasURL() {
resp.URL = userInfo.GetURL().String()
}
if userInfo.HasPhoto() {
resp.Photo = userInfo.GetPhoto().String()
}
if tkn.Scope.Has(domain.ScopeEmail) && userInfo.HasEmail() {
resp.Email = userInfo.GetEmail().String()
}
_ = encoder.Encode(resp) //nolint:errchkjson
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }

View File

@ -1,8 +1,36 @@
package http package http
import "source.toby3d.me/toby3d/auth/internal/domain"
type UserInformationResponse struct { type UserInformationResponse struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"` URL *domain.URL `json:"url,omitempty"`
Photo string `json:"photo,omitempty"` Photo *domain.URL `json:"photo,omitempty"`
Email string `json:"email,omitempty"` Email *domain.Email `json:"email,omitempty"`
}
func NewUserInformationResponse(in *domain.Profile, hasEmail bool) *UserInformationResponse {
out := new(UserInformationResponse)
if in == nil {
return out
}
if in.HasName() {
out.Name = in.GetName()
}
if in.HasURL() {
out.URL = &domain.URL{URL: in.GetURL()}
}
if in.HasPhoto() {
out.Photo = &domain.URL{URL: in.GetPhoto()}
}
if hasEmail {
out.Email = in.GetEmail()
}
return out
} }

View File

@ -7,6 +7,7 @@ import (
"testing" "testing"
"github.com/goccy/go-json" "github.com/goccy/go-json"
"github.com/google/go-cmp/cmp"
"source.toby3d.me/toby3d/auth/internal/common" "source.toby3d.me/toby3d/auth/internal/common"
"source.toby3d.me/toby3d/auth/internal/domain" "source.toby3d.me/toby3d/auth/internal/domain"
@ -57,14 +58,15 @@ func TestUserInfo(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if result.Name != deps.profile.GetName() || exp := &delivery.UserInformationResponse{
result.Photo != deps.profile.GetPhoto().String() { Name: deps.profile.GetName(),
t.Errorf("GET /userinfo = %+v, want %+v", result, &delivery.UserInformationResponse{ URL: &domain.URL{URL: deps.profile.GetURL()},
Name: deps.profile.GetName(), Photo: &domain.URL{URL: deps.profile.GetPhoto()},
URL: deps.profile.GetURL().String(), Email: deps.profile.GetEmail(),
Photo: deps.profile.GetPhoto().String(), }
Email: deps.profile.GetEmail().String(),
}) if diff := cmp.Diff(result, exp, cmp.AllowUnexported(domain.URL{}, domain.Email{})); diff != "" {
t.Errorf("%s %s = %+v, want %+v", req.Method, req.RequestURI, result, exp)
} }
} }