MCPcopy Create free account
hub / github.com/Effect-TS/effect / securityDecode

Function securityDecode

packages/effect/src/unstable/httpapi/HttpApiBuilder.ts:475–532  ·  view source on GitHub ↗
(
  self: Security
)

Source from the content-addressed store, hash-verified

473 * @since 4.0.0
474 */
475export const securityDecode = <Security extends HttpApiSecurity.HttpApiSecurity>(
476 self: Security
477): Effect.Effect<
478 HttpApiSecurity.HttpApiSecurity.Type<Security>,
479 never,
480 HttpServerRequest | Request.ParsedSearchParams
481> => {
482 switch (self._tag) {
483 case "Http": {
484 return Effect.map(
485 HttpServerRequest,
486 (request) =>
487 Redacted.make(
488 getAuthorizationCredential(request.headers.authorization, self.scheme) ?? ""
489 ) as any
490 )
491 }
492 case "ApiKey": {
493 const key = self.in === "header" ? self.key.toLowerCase() : self.key
494 const schema = Schema.Struct({
495 [key]: Schema.String
496 })
497 const decode: Effect.Effect<
498 { readonly [x: string]: string; readonly [x: number]: string },
499 Schema.SchemaError,
500 Request.ParsedSearchParams | HttpServerRequest
501 > = self.in === "query"
502 ? Request.schemaSearchParams(schema)
503 : self.in === "cookie"
504 ? Request.schemaCookies(schema)
505 : Request.schemaHeaders(schema)
506 return Effect.match(decode, {
507 onFailure: () => Redacted.make("") as any,
508 onSuccess: (match) => Redacted.make(match[key])
509 })
510 }
511 case "Basic": {
512 const empty: HttpApiSecurity.HttpApiSecurity.Type<Security> = {
513 username: "",
514 password: Redacted.make("")
515 } as any
516 return Effect.map(HttpServerRequest, (request) => {
517 const encoded = getAuthorizationCredential(request.headers.authorization, basicScheme)
518 if (encoded === undefined) return empty
519 const decoded = Result.getOrUndefined(Encoding.decodeBase64String(encoded))
520 if (decoded === undefined) return empty
521 // RFC 7617, Section 2: only the first colon separates the user-id from the password.
522 // https://www.rfc-editor.org/rfc/rfc7617.html#section-2
523 const separator = decoded.indexOf(":")
524 if (separator === -1) return empty
525 return {
526 username: decoded.slice(0, separator),
527 password: Redacted.make(decoded.slice(separator + 1))
528 } as any
529 })
530 }
531 }
532}

Callers 1

makeSecurityMiddlewareFunction · 0.85

Calls 3

makeMethod · 0.65
mapMethod · 0.45

Tested by

no test coverage detected