GetAuthServerMeta issues a GET request to retrieve authorization server metadata from an OAuth authorization server with the given metadataURL. It follows [RFC 8414]: - The metadataURL must use HTTPS or be a local address. - The Issuer field is checked against metadataURL.Issuer. It also verifies
(ctx context.Context, metadataURL, issuer string, c *http.Client)
| 131 | // |
| 132 | // [RFC 8414]: https://tools.ietf.org/html/rfc8414 |
| 133 | func GetAuthServerMeta(ctx context.Context, metadataURL, issuer string, c *http.Client) (*AuthServerMeta, error) { |
| 134 | // Only allow HTTP for local addresses (testing or development purposes). |
| 135 | if err := checkHTTPSOrLoopback(metadataURL); err != nil { |
| 136 | return nil, fmt.Errorf("metadataURL: %v", err) |
| 137 | } |
| 138 | asm, err := getJSON[AuthServerMeta](ctx, c, metadataURL, 1<<20) |
| 139 | if err != nil { |
| 140 | var httpErr *httpStatusError |
| 141 | if errors.As(err, &httpErr) { |
| 142 | if 400 <= httpErr.StatusCode && httpErr.StatusCode < 500 { |
| 143 | return nil, nil |
| 144 | } |
| 145 | } |
| 146 | return nil, fmt.Errorf("%v", err) // Do not expose error types. |
| 147 | } |
| 148 | if asm.Issuer != issuer { |
| 149 | // Validate the Issuer field (see RFC 8414, section 3.3). |
| 150 | return nil, fmt.Errorf("metadata issuer %q does not match issuer URL %q", asm.Issuer, issuer) |
| 151 | } |
| 152 | |
| 153 | if len(asm.CodeChallengeMethodsSupported) == 0 { |
| 154 | return nil, fmt.Errorf("authorization server at %s does not implement PKCE", issuer) |
| 155 | } |
| 156 | |
| 157 | // Validate endpoint URLs to prevent XSS attacks (see #526). |
| 158 | if err := validateAuthServerMetaURLs(asm); err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | |
| 162 | return asm, nil |
| 163 | } |
| 164 | |
| 165 | // validateAuthServerMetaURLs validates all URL fields in AuthServerMeta |
| 166 | // to ensure they don't use dangerous schemes that could enable XSS attacks. |
searching dependent graphs…