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

Allow for scopes when using ApiKeyAuth to support AWS API Gateway & Cognito #5181

Open
3 tasks done
thegagne opened this issue Nov 24, 2024 · 1 comment
Open
3 tasks done
Assignees
Labels
design:needed A design request has been raised that needs a proposal lib:http needs-area triaged:core
Milestone

Comments

@thegagne
Copy link

Clear and concise description of the problem

Goal: Create a way to use AWS API Gateway with Cognito as an authorizer, and define scopes.

I believe this is currently not possible as you cannot pass in scopes with ApiKeyAuth which is what AWS expects.

Something like

@extension("x-amazon-apigateway-authtype", "cognito_user_pools")
@extension("x-amazon-apigateway-authorizer", {
  type: "cognito_user_pools",
  providerARNs: ["arn:<redacted>"],
})
model CognitoAuth<Scopes> is ApiKeyAuth<ApiKeyLocation.header, "Authorization", Scopes>;


@post
@extension("x-amazon-apigateway-integration", {
  type: "http_proxy",
  httpMethod: "post",
  uri: "http://<redacted>",
  connectionId: vpcLinkId,
  connectionType: "VPC_LINK",
})
@useAuth(CognitoAuth<Scopes>)
post<CreateUser, User, VpcLinkId, ["user/:write"]>(CreateUser): {
  @statusCode statusCode: 201;
  @body _: User;
};

Example output:

   post:
      ...
      security:
      - CognitoAuth:
        - "users/:write"
      x-amazon-apigateway-integration:
        connectionId: "<redacted>"
        httpMethod: "GET"
        uri: "<redacted>"
        responses:
          default:
            statusCode: "201"
        connectionType: "VPC_LINK"
 ...
  securitySchemes:
    CognitoAuth:
      type: "apiKey"
      name: "Authorization"
      in: "header"
      x-amazon-apigateway-authtype: "cognito_user_pools"
      x-amazon-apigateway-authorizer:
        providerARNs:
        - "arn:<redacted>
        type: "cognito_user_pools"

See https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-enable-cognito-user-pool.html

Checklist

  • Follow our Code of Conduct
  • Read the docs.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
@markcowl markcowl added the design:needed A design request has been raised that needs a proposal label Nov 25, 2024
@markcowl markcowl added this to the Backlog milestone Nov 25, 2024
@thegagne
Copy link
Author

thegagne commented Nov 26, 2024

I got it somewhat working with the following changes:

@typespec/http/dist/src/types.d.ts
23c23
< export type HttpAuth = BasicAuth | BearerAuth | ApiKeyAuth<ApiKeyLocation, string> | Oauth2Auth<OAuth2Flow[]> | OpenIDConnectAuth | NoAuth;
---
> export type HttpAuth = BasicAuth | BearerAuth | ApiKeyAuth<ApiKeyLocation, string, string[]> | Oauth2Auth<OAuth2Flow[]> | OpenIDConnectAuth | NoAuth;
84a85
>     scopes: OAuth2Scope[];
@typespec/openapi3/dist/src/openapi.js
1251a1252
>                 console.log(httpAuthRef);
1257a1259,1263
>                         continue;
>                     case "any":
>                         if (httpAuthRef.auth.type === "apiKey") {
>                             securityOption[httpAuthRef.auth.id] = httpAuthRef.auth.defaultScopes;
>                         }
@typespec/http/lib/auth.tsp
92c92
< model ApiKeyAuth<Location extends ApiKeyLocation, Name extends string> {
---
> model ApiKeyAuth<Location extends ApiKeyLocation, Name extends string, Scopes extends string[] = []> {
100a101,103
> 
>   @doc("Scopes of every flow. Overridden by scope definitions in specific flows")
>   defaultScopes: Scopes;

I did not check that this didn't break existing usage of ApiKeyAuth or anything beyond just compiling, visually checking it, and trying to use the import feature on API Gateway, which seemed to work.

Leaving this here in case someone else picks it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design:needed A design request has been raised that needs a proposal lib:http needs-area triaged:core
Projects
None yet
Development

No branches or pull requests

4 participants