♻️ Refactored user HTTP delivery schema
This commit is contained in:
parent
e7f86d5de9
commit
bf3499084e
4 changed files with 49 additions and 40 deletions
|
@ -154,11 +154,8 @@ func (h *Handler) handleAuthorize(w http.ResponseWriter, r *http.Request) {
|
|||
w.WriteHeader(http.StatusBadRequest)
|
||||
web.WriteTemplate(w, &web.ErrorPage{
|
||||
BaseOf: baseOf,
|
||||
Error: domain.NewError(
|
||||
domain.ErrorCodeInvalidClient,
|
||||
"requested redirect_uri is not registered on client_id side",
|
||||
"",
|
||||
),
|
||||
Error: domain.NewError(domain.ErrorCodeInvalidClient, "requested redirect_uri is not"+
|
||||
" registered on client_id side", ""),
|
||||
})
|
||||
|
||||
return
|
||||
|
|
|
@ -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
|
||||
// MUST return a 200 Response.
|
||||
_ = encoder.Encode(err) //nolint:errchkjson
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
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",
|
||||
"https://indieauth.net/source/#user-information",
|
||||
))
|
||||
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
resp := new(UserInformationResponse)
|
||||
if userInfo == nil {
|
||||
_ = encoder.Encode(resp) //nolint:errchkjson
|
||||
//nolint:errchkjson
|
||||
_ = encoder.Encode(NewUserInformationResponse(userInfo,
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -1,8 +1,36 @@
|
|||
package http
|
||||
|
||||
import "source.toby3d.me/toby3d/auth/internal/domain"
|
||||
|
||||
type UserInformationResponse struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
URL string `json:"url,omitempty"`
|
||||
Photo string `json:"photo,omitempty"`
|
||||
Email string `json:"email,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
URL *domain.URL `json:"url,omitempty"`
|
||||
Photo *domain.URL `json:"photo,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
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
||||
"source.toby3d.me/toby3d/auth/internal/common"
|
||||
"source.toby3d.me/toby3d/auth/internal/domain"
|
||||
|
@ -57,14 +58,15 @@ func TestUserInfo(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if result.Name != deps.profile.GetName() ||
|
||||
result.Photo != deps.profile.GetPhoto().String() {
|
||||
t.Errorf("GET /userinfo = %+v, want %+v", result, &delivery.UserInformationResponse{
|
||||
Name: deps.profile.GetName(),
|
||||
URL: deps.profile.GetURL().String(),
|
||||
Photo: deps.profile.GetPhoto().String(),
|
||||
Email: deps.profile.GetEmail().String(),
|
||||
})
|
||||
exp := &delivery.UserInformationResponse{
|
||||
Name: deps.profile.GetName(),
|
||||
URL: &domain.URL{URL: deps.profile.GetURL()},
|
||||
Photo: &domain.URL{URL: deps.profile.GetPhoto()},
|
||||
Email: deps.profile.GetEmail(),
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue