-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathprotected-action-validator.ts
197 lines (167 loc) · 5.35 KB
/
protected-action-validator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { SpanKind, SpanStatusCode } from "@opentelemetry/api";
import { APL, AuthData } from "@/APL";
import { verifyJWT } from "@/auth/verify-jwt";
import { createDebug } from "@/debug";
import { getOtelTracer } from "@/open-telemetry";
import { Permission } from "@/types";
import { extractUserFromJwt, TokenUserPayload } from "@/util/extract-user-from-jwt";
import { ActionHandlerResult, PlatformAdapterInterface } from "./generic-adapter-use-case-types";
import { SaleorRequestProcessor } from "./saleor-request-processor";
export type ProtectedHandlerConfig = {
apl: APL;
requiredPermissions?: Permission[];
};
export type ProtectedHandlerContext = {
baseUrl: string;
authData: AuthData;
user: TokenUserPayload;
};
export type ValidationResult =
| { result: "failure"; value: ActionHandlerResult }
| { result: "ok"; value: ProtectedHandlerContext };
export class ProtectedActionValidator<I> {
private debug = createDebug("ProtectedActionValidator");
private tracer = getOtelTracer();
constructor(private adapter: PlatformAdapterInterface<I>) {}
private requestProcessor = new SaleorRequestProcessor(this.adapter);
/** Validates received request if it's legitimate webhook request from Saleor
* returns ActionHandlerResult if request is invalid and must be terminated early
* */
async validateRequest(config: ProtectedHandlerConfig): Promise<ValidationResult> {
return this.tracer.startActiveSpan(
"processSaleorProtectedHandler",
{
kind: SpanKind.INTERNAL,
attributes: {
requiredPermissions: config.requiredPermissions,
},
},
async (span): Promise<ValidationResult> => {
this.debug("Request processing started");
const { saleorApiUrl, authorizationBearer: token } =
this.requestProcessor.getSaleorHeaders();
const baseUrl = this.adapter.getBaseUrl();
span.setAttribute("saleorApiUrl", saleorApiUrl ?? "");
if (!baseUrl) {
span
.setStatus({
code: SpanStatusCode.ERROR,
message: "Missing host header",
})
.end();
this.debug("Missing host header");
return {
result: "failure",
value: {
bodyType: "string",
status: 400,
body: "Validation error: Missing host header",
},
};
}
if (!saleorApiUrl) {
span
.setStatus({
code: SpanStatusCode.ERROR,
message: "Missing saleor-api-url header",
})
.end();
this.debug("Missing saleor-api-url header");
return {
result: "failure",
value: {
bodyType: "string",
status: 400,
body: "Validation error: Missing saleor-api-url header",
},
};
}
if (!token) {
span
.setStatus({
code: SpanStatusCode.ERROR,
message: "Missing authorization-bearer header",
})
.end();
this.debug("Missing authorization-bearer header");
return {
result: "failure",
value: {
bodyType: "string",
status: 400,
body: "Validation error: Missing authorization-bearer header",
},
};
}
// Check if API URL has been registered in the APL
const authData = await config.apl.get(saleorApiUrl);
if (!authData) {
span
.setStatus({
code: SpanStatusCode.ERROR,
message: "APL didn't found auth data for API URL",
})
.end();
this.debug("APL didn't found auth data for API URL %s", saleorApiUrl);
return {
result: "failure",
value: {
bodyType: "string",
status: 401,
body: `Validation error: Can't find auth data for saleorApiUrl ${saleorApiUrl}. Please register the application`,
},
};
}
try {
await verifyJWT({
appId: authData.appId,
token,
saleorApiUrl,
requiredPermissions: config.requiredPermissions,
});
} catch (e) {
span
.setStatus({
code: SpanStatusCode.ERROR,
message: "JWT verification failed",
})
.end();
return {
result: "failure",
value: {
bodyType: "string",
status: 401,
body: "Validation error: JWT verification failed",
},
};
}
try {
const userJwtPayload = extractUserFromJwt(token);
span.end();
return {
result: "ok",
value: {
baseUrl,
authData,
user: userJwtPayload,
},
};
} catch (err) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: "Error parsing user from JWT",
});
span.end();
return {
result: "failure",
value: {
bodyType: "string",
status: 500,
body: "Unexpected error: parsing user from JWT",
},
};
}
},
);
}
}