this repo has no description

simplify unmarshaling

Changed files
+2 -39
+2 -12
oauth.go
···
return "", fmt.Errorf("received non-200 response from pds. code was %d", resp.StatusCode)
}
-
b, err := io.ReadAll(resp.Body)
-
if err != nil {
-
return "", fmt.Errorf("could not read body: %w", err)
-
}
-
var resource OauthProtectedResource
-
if err := resource.UnmarshalJSON(b); err != nil {
+
if err := json.NewDecoder(resp.Body).Decode(&resource); err != nil {
return "", fmt.Errorf("could not unmarshal json: %w", err)
}
···
return nil, fmt.Errorf("received non-200 response from pds. status code was %d", resp.StatusCode)
}
-
b, err := io.ReadAll(resp.Body)
-
if err != nil {
-
return nil, fmt.Errorf("could not read body for authserver metadata response: %w", err)
-
}
-
var metadata OauthAuthorizationMetadata
-
if err := metadata.UnmarshalJSON(b); err != nil {
+
if err := json.NewDecoder(resp.Body).Decode(&metadata); err != nil {
return nil, fmt.Errorf("could not unmarshal authserver metadata: %w", err)
}
-27
types.go
···
package oauth
import (
-
"encoding/json"
"fmt"
"net/url"
"slices"
···
ScopesSupported []string `json:"scopes_supported"`
BearerMethodsSupported []string `json:"bearer_methods_supported"`
ResourceDocumentation string `json:"resource_documentation"`
-
}
-
-
func (opr *OauthProtectedResource) UnmarshalJSON(b []byte) error {
-
type Tmp OauthProtectedResource
-
var tmp Tmp
-
-
if err := json.Unmarshal(b, &tmp); err != nil {
-
return err
-
}
-
-
*opr = OauthProtectedResource(tmp)
-
-
return nil
}
type OauthAuthorizationMetadata struct {
···
DpopSigningAlgValuesSupported []string `json:"dpop_signing_alg_values_supported"`
ProtectedResources []string `json:"protected_resources"`
ClientIDMetadataDocumentSupported bool `json:"client_id_metadata_document_supported"`
-
}
-
-
func (oam *OauthAuthorizationMetadata) UnmarshalJSON(b []byte) error {
-
type Tmp OauthAuthorizationMetadata
-
var tmp Tmp
-
-
if err := json.Unmarshal(b, &tmp); err != nil {
-
return err
-
}
-
-
*oam = OauthAuthorizationMetadata(tmp)
-
-
return nil
}
func (oam *OauthAuthorizationMetadata) Validate(fetch_url *url.URL) error {