add option to disable some URL checks #2

merged
opened by winter.bsky.social targeting main from winter.bsky.social/atproto-oauth: push-uqplmsuzttpn

This is useful for local development.

Changed files
+18 -13
helpers
+3 -3
helpers/generic.go
···
return key, nil
}
-
func IsUrlSafeAndParsed(ustr string) (*url.URL, error) {
+
func IsUrlSafeAndParsed(ustr string, insecure bool) (*url.URL, error) {
u, err := url.Parse(ustr)
if err != nil {
return nil, err
}
-
if u.Scheme != "https" {
+
if u.Scheme != "https" && !insecure {
return nil, fmt.Errorf("input url is not https")
}
···
return nil, fmt.Errorf("url user was not empty")
}
-
if u.Port() != "" {
+
if u.Port() != "" && !insecure {
return nil, fmt.Errorf("url port was not empty")
}
+7 -4
oauth.go
···
clientKid string
clientId string
redirectUri string
+
insecure bool
}
type ClientArgs struct {
···
ClientJwk jwk.Key
ClientId string
RedirectUri string
+
Insecure bool
}
func NewClient(args ClientArgs) (*Client, error) {
···
clientPrivateKey: clientPkey,
clientId: args.ClientId,
redirectUri: args.RedirectUri,
+
insecure: args.Insecure,
}, nil
}
func (c *Client) ResolvePdsAuthServer(ctx context.Context, ustr string) (string, error) {
-
u, err := helpers.IsUrlSafeAndParsed(ustr)
+
u, err := helpers.IsUrlSafeAndParsed(ustr, c.insecure)
if err != nil {
return "", err
}
···
}
func (c *Client) FetchAuthServerMetadata(ctx context.Context, ustr string) (*OauthAuthorizationMetadata, error) {
-
u, err := helpers.IsUrlSafeAndParsed(ustr)
+
u, err := helpers.IsUrlSafeAndParsed(ustr, c.insecure)
if err != nil {
return nil, err
}
···
return nil, fmt.Errorf("could not unmarshal authserver metadata: %w", err)
}
-
if err := metadata.Validate(u); err != nil {
+
if err := metadata.Validate(u, c.insecure); err != nil {
return nil, fmt.Errorf("could not validate authserver metadata: %w", err)
}
···
params.Set("login_hint", loginHint)
}
-
_, err = helpers.IsUrlSafeAndParsed(parUrl)
+
_, err = helpers.IsUrlSafeAndParsed(parUrl, c.insecure)
if err != nil {
return nil, err
}
+8 -6
types.go
···
return nil
}
-
func (oam *OauthAuthorizationMetadata) Validate(fetch_url *url.URL) error {
+
func (oam *OauthAuthorizationMetadata) Validate(fetch_url *url.URL, insecure bool) error {
if fetch_url == nil {
return fmt.Errorf("fetch_url was nil")
}
···
return fmt.Errorf("issuer hostname does not match fetch url hostname")
}
-
if iu.Scheme != "https" {
-
return fmt.Errorf("issuer url is not https")
-
}
+
if !insecure {
+
if iu.Scheme != "https" {
+
return fmt.Errorf("issuer url is not https")
+
}
-
if iu.Port() != "" {
-
return fmt.Errorf("issuer port is not empty")
+
if iu.Port() != "" {
+
return fmt.Errorf("issuer port is not empty")
+
}
}
if iu.Path != "" && iu.Path != "/" {