Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make all request parameters (url, method, body) accessible for RequestHandler.prepareRequest #1554

Open
qoomon opened this issue Oct 9, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@qoomon
Copy link

qoomon commented Oct 9, 2023

I need to sign my requests with SignatureV4 to access some AWS_IAM secured APIs.

To sign a request I need to access target url, method, headers and the body data (see ky hook example implementation below).

I'd like to do the same with the @actions/http-client, however currently I can't access url, method and the body data in the RequestHandler.prepareRequest(options: http.RequestOptions):void hook, see https://github.com/actions/toolkit/blob/797f48fcfac3bcc50833df1ff0d35b16d7401503/packages/http-client/src/interfaces.ts#L57C1-L57C1

So make url, method and body data accessible to RequestHandler.prepareRequest(options: http.RequestOptions):void as well.

hy hook example of SignatureV4 signer implementation

import {SignatureV4} from "@smithy/signature-v4";
import {HttpRequest} from "@smithy/protocol-http";
import {BeforeRequestHook} from "ky";

export function AwsRequestSigner(signer: SignatureV4): BeforeRequestHook {
    return async (request: Request) => {
        const requestUrl = new URL(request.url);
        const requestHeaders = Object.fromEntries(request.headers.entries());
        if (requestHeaders["Authorization"]) {
            // preserve Authorization header
            requestHeaders["X-Authorization"] = requestHeaders["Authorization"];
            delete requestHeaders["Authorization"];
        }
        const canonicalRequest = new HttpRequest({
            hostname: requestUrl.hostname,
            // port: requestUrl.port ? parseInt(requestUrl.port) : undefined,
            path: requestUrl.pathname,
            protocol: requestUrl.protocol,
            method: request.method,
            body: await request.text(),
            query: Object.fromEntries(requestUrl.searchParams.entries()),
            headers: {
                ...requestHeaders,
                "Host": requestUrl.host,
            },
        });

        const signedCanonicalRequest = await signer.sign(canonicalRequest);

        return new Request(request, {
            headers: signedCanonicalRequest.headers,
            body: signedCanonicalRequest.body,
        })
    }
}
@qoomon qoomon added the enhancement New feature or request label Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant