| 1018 | * @category security |
| 1019 | */ |
| 1020 | export const securityDecode = <Security extends HttpApiSecurity.HttpApiSecurity>( |
| 1021 | self: Security |
| 1022 | ): Effect.Effect< |
| 1023 | HttpApiSecurity.HttpApiSecurity.Type<Security>, |
| 1024 | never, |
| 1025 | HttpServerRequest.HttpServerRequest | HttpServerRequest.ParsedSearchParams |
| 1026 | > => { |
| 1027 | switch (self._tag) { |
| 1028 | case "Bearer": { |
| 1029 | return Effect.map( |
| 1030 | HttpServerRequest.HttpServerRequest, |
| 1031 | (request) => Redacted.make((request.headers.authorization ?? "").slice(bearerLen)) as any |
| 1032 | ) |
| 1033 | } |
| 1034 | case "ApiKey": { |
| 1035 | const key = self.in === "header" ? self.key.toLowerCase() : self.key |
| 1036 | const schema = Schema.Struct({ |
| 1037 | [key]: Schema.String |
| 1038 | }) |
| 1039 | const decode = unify( |
| 1040 | self.in === "query" |
| 1041 | ? HttpServerRequest.schemaSearchParams(schema) |
| 1042 | : self.in === "cookie" |
| 1043 | ? HttpServerRequest.schemaCookies(schema) |
| 1044 | : HttpServerRequest.schemaHeaders(schema) |
| 1045 | ) |
| 1046 | return Effect.match(decode, { |
| 1047 | onFailure: () => Redacted.make("") as any, |
| 1048 | onSuccess: (match) => Redacted.make(match[key]) |
| 1049 | }) |
| 1050 | } |
| 1051 | case "Basic": { |
| 1052 | const empty: HttpApiSecurity.HttpApiSecurity.Type<Security> = { |
| 1053 | username: "", |
| 1054 | password: Redacted.make("") |
| 1055 | } as any |
| 1056 | return HttpServerRequest.HttpServerRequest.pipe( |
| 1057 | Effect.flatMap((request) => Encoding.decodeBase64String((request.headers.authorization ?? "").slice(basicLen))), |
| 1058 | Effect.match({ |
| 1059 | onFailure: () => empty, |
| 1060 | onSuccess: (header) => { |
| 1061 | const parts = header.split(":") |
| 1062 | if (parts.length !== 2) { |
| 1063 | return empty |
| 1064 | } |
| 1065 | return { |
| 1066 | username: parts[0], |
| 1067 | password: Redacted.make(parts[1]) |
| 1068 | } as any |
| 1069 | } |
| 1070 | }) |
| 1071 | ) |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | /** |
| 1077 | * Set a cookie from an `HttpApiSecurity.HttpApiKey` instance. |