回答編集履歴
1
コードを記載
test
CHANGED
@@ -1 +1,131 @@
|
|
1
1
|
502(Bad Gateway)の原因は「"statusCode": "200"」でした。「"statusCode": 200」と書かなければならなかったようです。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
最終的には以下の実装でうまくいきました。
|
6
|
+
|
7
|
+
```python
|
8
|
+
|
9
|
+
def lambda_handler(event, context):
|
10
|
+
|
11
|
+
"""
|
12
|
+
|
13
|
+
ラムダハンドラ
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
Args:
|
18
|
+
|
19
|
+
event (dict): イベント
|
20
|
+
|
21
|
+
context (object): コンテキスト
|
22
|
+
|
23
|
+
"""
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
del context
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
try:
|
32
|
+
|
33
|
+
# 開始ログ
|
34
|
+
|
35
|
+
logging.info("'basic_authentication' Lambda start", extra={"event": event})
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
# BASIC認証ユーザIDとパスワード
|
40
|
+
|
41
|
+
ssm = SSM()
|
42
|
+
|
43
|
+
user_id = ssm.get_parameter_store_value(lambda_env.get("BASIC_AUTH_ID_PATH"))
|
44
|
+
|
45
|
+
password = ssm.get_parameter_store_value(lambda_env.get("BASIC_AUTH_PASSWORD_PATH"))
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
# 認証成功時払い出しCookie情報
|
50
|
+
|
51
|
+
cookie_name = ssm.get_parameter_store_value(lambda_env.get("COOKIE_NAME_PATH"))
|
52
|
+
|
53
|
+
cookie_value = ssm.get_parameter_store_value(lambda_env.get("COOKIE_VALUE_PATH"))
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
# 認証成功時のリダイレクト先URL
|
58
|
+
|
59
|
+
redirect_url = lambda_env.get("REDIRECT_URL")
|
60
|
+
|
61
|
+
# 認証文字列
|
62
|
+
|
63
|
+
auth_str = base64.b64encode(f"{user_id}:{password}".encode("utf-8")).decode("ascii")
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
headers = event["headers"]
|
68
|
+
|
69
|
+
if ("authorization" not in headers) or (headers["authorization"].split()[1] != auth_str):
|
70
|
+
|
71
|
+
# Basic認証要求
|
72
|
+
|
73
|
+
return {
|
74
|
+
|
75
|
+
"statusCode": 401,
|
76
|
+
|
77
|
+
"statusDescription": "401 Unauthorized",
|
78
|
+
|
79
|
+
"body": "Unauthorized",
|
80
|
+
|
81
|
+
"isBase64Encoded": False,
|
82
|
+
|
83
|
+
"headers": {
|
84
|
+
|
85
|
+
"WWW-Authenticate": "Basic",
|
86
|
+
|
87
|
+
"Content-Type": "text/html"
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
# Basic認証成功
|
96
|
+
|
97
|
+
return {
|
98
|
+
|
99
|
+
"statusCode": 302,
|
100
|
+
|
101
|
+
"statusDescription": "302 Found",
|
102
|
+
|
103
|
+
"isBase64Encoded": False,
|
104
|
+
|
105
|
+
"headers": {
|
106
|
+
|
107
|
+
"Set-cookie": f"{cookie_name}={cookie_value}",
|
108
|
+
|
109
|
+
"location": redirect_url
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
except Exception as e:
|
116
|
+
|
117
|
+
# 例外発生
|
118
|
+
|
119
|
+
logging.exception("An error occurred with 'basic_authentication' Lambda")
|
120
|
+
|
121
|
+
raise e
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
finally:
|
126
|
+
|
127
|
+
# 終了ログ
|
128
|
+
|
129
|
+
logging.info("'basic_authentication' Lambda end", extra={"event": event})
|
130
|
+
|
131
|
+
```
|