Skip to content

Commit fb9f767

Browse files
committed
chore: add swagger example
1 parent dadf08a commit fb9f767

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

examples/event_handler_rest/sam/swagger_ui_oauth2_template.yaml

+57-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,67 @@ Resources:
1818
HelloWorldFunction:
1919
Type: AWS::Serverless::Function
2020
Properties:
21-
CodeUri: hello_world/
21+
CodeUri: ../src
2222
Handler: swagger_ui_oauth2.lambda_handler
23+
Environment:
24+
Variables:
25+
COGNITO_USER_POOL_DOMAIN: !Ref UserPoolDomain
2326
Events:
2427
AnyApiEvent:
2528
Type: Api
2629
Properties:
2730
Path: /{proxy+} # Send requests on any path to the lambda function
2831
Method: ANY # Send requests using any http method to the lambda function
32+
33+
CognitoUserPool:
34+
Type: AWS::Cognito::UserPool
35+
Properties:
36+
UserPoolName: PowertoolsUserPool
37+
Policies:
38+
PasswordPolicy:
39+
MinimumLength: 8
40+
RequireLowercase: true
41+
RequireNumbers: true
42+
RequireSymbols: true
43+
RequireUppercase: true
44+
45+
CognitoUserPoolClient:
46+
Type: AWS::Cognito::UserPoolClient
47+
Properties:
48+
ClientName: PowertoolsClient
49+
UserPoolId: !Ref CognitoUserPool
50+
GenerateSecret: true
51+
RefreshTokenValidity: 30
52+
ExplicitAuthFlows:
53+
- ALLOW_USER_PASSWORD_AUTH
54+
- ALLOW_REFRESH_TOKEN_AUTH
55+
SupportedIdentityProviders:
56+
- COGNITO
57+
CallbackURLs:
58+
# NOTE: for this to work, your OAuth2 redirect url needs to precisely follow this format:
59+
# https://<your_api_id>.execute-api.<region>.amazonaws.com/<stage>/swagger?format=oauth2-redirect
60+
- !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/${ServerlessRestApi.Stage}/swagger?format=oauth2-redirect"
61+
AllowedOAuthFlows:
62+
- code
63+
AllowedOAuthScopes:
64+
- openid
65+
- email
66+
- profile
67+
- aws.cognito.signin.user.admin
68+
AllowedOAuthFlowsUserPoolClient: true
69+
70+
UserPoolDomain:
71+
Type: AWS::Cognito::UserPoolDomain
72+
Properties:
73+
Domain: powertools-swagger-oauth2
74+
UserPoolId: !Ref CognitoUserPool
75+
76+
Outputs:
77+
HelloWorldApiUrl:
78+
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/${ServerlessRestApi.Stage}/swagger"
79+
80+
CognitoOAuthClientId:
81+
Value: !GetAtt CognitoUserPoolClient.ClientId
82+
83+
CognitoDomain:
84+
Value: !Ref UserPoolDomain

examples/event_handler_rest/src/swagger_ui_oauth2.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
from aws_lambda_powertools import Logger, Tracer
24
from aws_lambda_powertools.event_handler import (
35
APIGatewayRestResolver,
@@ -13,24 +15,20 @@
1315
tracer = Tracer()
1416
logger = Logger()
1517

16-
oauth2 = OAuth2Config(
17-
client_id="your_oauth2_client_id",
18-
client_secret="your_oauth2_secret",
19-
app_name="OAuth2 Test",
20-
)
18+
region = os.getenv("AWS_REGION")
19+
cognito_domain = os.getenv("COGNITO_USER_POOL_DOMAIN")
2120

2221
app = APIGatewayRestResolver(enable_validation=True)
23-
24-
# NOTE: for this to work, your OAuth2 redirect url needs to precisely follow this format:
25-
# https://<your_api_id>.execute-api.<region>.amazonaws.com/<stage>/swagger?format=oauth2-redirect
2622
app.enable_swagger(
27-
oauth2_config=oauth2,
23+
# NOTE: for this to work, your OAuth2 redirect url needs to precisely follow this format:
24+
# https://<your_api_id>.execute-api.<region>.amazonaws.com/<stage>/swagger?format=oauth2-redirect
25+
oauth2_config=OAuth2Config(app_name="OAuth2 Test"),
2826
security_schemes={
2927
"oauth": OAuth2(
3028
flows=OAuthFlows(
3129
authorizationCode=OAuthFlowAuthorizationCode(
32-
authorizationUrl="https://your-cognito-domain.eu-central-1.amazoncognito.com/oauth2/authorize",
33-
tokenUrl="https://your-cognito-domain.eu-central-1.amazoncognito.com/oauth2/token",
30+
authorizationUrl=f"https://{cognito_domain}.auth.{region}.amazoncognito.com/oauth2/authorize",
31+
tokenUrl=f"https://{cognito_domain}.auth.{region}.amazoncognito.com/oauth2/token",
3432
),
3533
),
3634
),

0 commit comments

Comments
 (0)