move response structs out of function

This commit is contained in:
Niko Abeler 2022-11-08 20:07:21 +01:00
parent b65230db88
commit f377ce032a
1 changed files with 33 additions and 30 deletions

View File

@ -11,6 +11,35 @@ import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
type IndieauthMetaDataResponse struct {
Issuer string `json:"issuer"`
AuthorizationEndpoint string `json:"authorization_endpoint"`
TokenEndpoint string `json:"token_endpoint"`
CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported"`
ScopesSupported []string `json:"scopes_supported"`
ResponseTypesSupported []string `json:"response_types_supported"`
GrantTypesSupported []string `json:"grant_types_supported"`
}
type MeProfileResponse struct {
Name string `json:"name"`
Url string `json:"url"`
Photo string `json:"photo"`
}
type MeResponse struct {
Me string `json:"me"`
Profile MeProfileResponse `json:"profile"`
}
type AccessTokenResponse struct {
Me string `json:"me"`
TokenType string `json:"token_type"`
AccessToken string `json:"access_token"`
Scope string `json:"scope"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
}
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)
@ -20,16 +49,7 @@ func userAuthMetadataHandler(repo *owl.Repository) func(http.ResponseWriter, *ht
return return
} }
type Response struct { response := IndieauthMetaDataResponse{
Issuer string `json:"issuer"`
AuthorizationEndpoint string `json:"authorization_endpoint"`
TokenEndpoint string `json:"token_endpoint"`
CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported"`
ScopesSupported []string `json:"scopes_supported"`
ResponseTypesSupported []string `json:"response_types_supported"`
GrantTypesSupported []string `json:"grant_types_supported"`
}
response := Response{
Issuer: user.FullUrl(), Issuer: user.FullUrl(),
AuthorizationEndpoint: user.AuthUrl(), AuthorizationEndpoint: user.AuthUrl(),
TokenEndpoint: user.TokenUrl(), TokenEndpoint: user.TokenUrl(),
@ -225,18 +245,9 @@ 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)
type ResponseProfile struct { response := MeResponse{
Name string `json:"name"`
Url string `json:"url"`
Photo string `json:"photo"`
}
type Response struct {
Me string `json:"me"`
Profile ResponseProfile `json:"profile"`
}
response := Response{
Me: user.FullUrl(), Me: user.FullUrl(),
Profile: ResponseProfile{ Profile: MeProfileResponse{
Name: user.Name(), Name: user.Name(),
Url: user.FullUrl(), Url: user.FullUrl(),
Photo: user.AvatarUrl(), Photo: user.AvatarUrl(),
@ -271,14 +282,6 @@ func userAuthTokenHandler(repo *owl.Repository) func(http.ResponseWriter, *http.
return return
} }
type Response struct {
Me string `json:"me"`
TokenType string `json:"token_type"`
AccessToken string `json:"access_token"`
Scope string `json:"scope"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
}
accessToken, duration, err := user.GenerateAccessToken(authCode) accessToken, duration, err := user.GenerateAccessToken(authCode)
if err != nil { if err != nil {
println("Error generating access token: ", err.Error()) println("Error generating access token: ", err.Error())
@ -286,7 +289,7 @@ func userAuthTokenHandler(repo *owl.Repository) func(http.ResponseWriter, *http.
w.Write([]byte("Internal server error")) w.Write([]byte("Internal server error"))
return return
} }
response := Response{ response := AccessTokenResponse{
Me: user.FullUrl(), Me: user.FullUrl(),
TokenType: "Bearer", TokenType: "Bearer",
AccessToken: accessToken, AccessToken: accessToken,