refactor handler

This commit is contained in:
Niko Abeler 2022-11-08 20:10:50 +01:00
parent f377ce032a
commit fbf14704e7
1 changed files with 18 additions and 28 deletions

View File

@ -40,6 +40,17 @@ type AccessTokenResponse struct {
RefreshToken string `json:"refresh_token"` RefreshToken string `json:"refresh_token"`
} }
func jsonResponse(w http.ResponseWriter, response interface{}) {
jsonData, err := json.Marshal(response)
if err != nil {
println("Error marshalling json: ", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
}
w.Header().Add("Content-Type", "application/json")
w.Write(jsonData)
}
func userAuthMetadataHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) { func userAuthMetadataHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
user, err := getUserFromRepo(repo, ps) user, err := getUserFromRepo(repo, ps)
@ -48,8 +59,8 @@ func userAuthMetadataHandler(repo *owl.Repository) func(http.ResponseWriter, *ht
notFoundHandler(repo)(w, r) notFoundHandler(repo)(w, r)
return return
} }
w.WriteHeader(http.StatusOK)
response := IndieauthMetaDataResponse{ jsonResponse(w, IndieauthMetaDataResponse{
Issuer: user.FullUrl(), Issuer: user.FullUrl(),
AuthorizationEndpoint: user.AuthUrl(), AuthorizationEndpoint: user.AuthUrl(),
TokenEndpoint: user.TokenUrl(), TokenEndpoint: user.TokenUrl(),
@ -57,14 +68,7 @@ func userAuthMetadataHandler(repo *owl.Repository) func(http.ResponseWriter, *ht
ScopesSupported: []string{"profile"}, ScopesSupported: []string{"profile"},
ResponseTypesSupported: []string{"code"}, ResponseTypesSupported: []string{"code"},
GrantTypesSupported: []string{"authorization_code"}, GrantTypesSupported: []string{"authorization_code"},
} })
jsonData, err := json.Marshal(response)
if err != nil {
println("Error marshalling json: ", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
}
w.Write(jsonData)
} }
} }
@ -245,21 +249,14 @@ func userAuthProfileHandler(repo *owl.Repository) func(http.ResponseWriter, *htt
valid, _ := verifyAuthCodeRequest(user, w, r) valid, _ := verifyAuthCodeRequest(user, w, r)
if valid { if valid {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
response := MeResponse{ jsonResponse(w, MeResponse{
Me: user.FullUrl(), Me: user.FullUrl(),
Profile: MeProfileResponse{ Profile: MeProfileResponse{
Name: user.Name(), Name: user.Name(),
Url: user.FullUrl(), Url: user.FullUrl(),
Photo: user.AvatarUrl(), Photo: user.AvatarUrl(),
}, },
} })
jsonData, err := json.Marshal(response)
if err != nil {
println("Error marshalling json: ", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
}
w.Write(jsonData)
return return
} }
} }
@ -289,20 +286,13 @@ func userAuthTokenHandler(repo *owl.Repository) func(http.ResponseWriter, *http.
w.Write([]byte("Internal server error")) w.Write([]byte("Internal server error"))
return return
} }
response := AccessTokenResponse{ jsonResponse(w, AccessTokenResponse{
Me: user.FullUrl(), Me: user.FullUrl(),
TokenType: "Bearer", TokenType: "Bearer",
AccessToken: accessToken, AccessToken: accessToken,
Scope: authCode.Scope, Scope: authCode.Scope,
ExpiresIn: duration, ExpiresIn: duration,
} })
jsonData, err := json.Marshal(response)
if err != nil {
println("Error marshalling json: ", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
}
w.Write(jsonData)
return return
} }
} }