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

Function securityDecode

packages/effect/src/http-api/HttpApiBuilder.ts:569–626  ·  view source on GitHub ↗
(
  self: Security
)

Source from the content-addressed store, hash-verified

567 * @since 4.0.0
568 */
569export const securityDecode = <Security extends HttpApiSecurity.HttpApiSecurity>(
570 self: Security
571): Effect.Effect<
572 HttpApiSecurity.HttpApiSecurity.Type<Security>,
573 never,
574 HttpServerRequest | Request.ParsedSearchParams
575> => {
576 switch (self._tag) {
577 case "Http": {
578 return Effect.map(
579 HttpServerRequest,
580 (request) =>
581 Redacted.make(
582 getAuthorizationCredential(request.headers.authorization, self.scheme) ?? ""
583 ) as any
584 )
585 }
586 case "ApiKey": {
587 const key = self.in === "header" ? self.key.toLowerCase() : self.key
588 const schema = Schema.Struct({
589 [key]: Schema.String
590 })
591 const decode: Effect.Effect<
592 { readonly [x: string]: string; readonly [x: number]: string },
593 Schema.SchemaError,
594 Request.ParsedSearchParams | HttpServerRequest
595 > = self.in === "query"
596 ? Request.schemaSearchParams(schema)
597 : self.in === "cookie"
598 ? Request.schemaCookies(schema)
599 : Request.schemaHeaders(schema)
600 return Effect.match(decode, {
601 onFailure: () => Redacted.make("") as any,
602 onSuccess: (match) => Redacted.make(match[key])
603 })
604 }
605 case "Basic": {
606 const empty: HttpApiSecurity.HttpApiSecurity.Type<Security> = {
607 username: "",
608 password: Redacted.make("")
609 } as any
610 return Effect.map(HttpServerRequest, (request) => {
611 const encoded = getAuthorizationCredential(request.headers.authorization, basicScheme)
612 if (encoded === undefined) return empty
613 const decoded = Result.getOrUndefined(Base64.decodeString(encoded))
614 if (decoded === undefined) return empty
615 // RFC 7617, Section 2: only the first colon separates the user-id from the password.
616 // https://www.rfc-editor.org/rfc/rfc7617.html#section-2
617 const separator = decoded.indexOf(":")
618 if (separator === -1) return empty
619 return {
620 username: decoded.slice(0, separator),
621 password: Redacted.make(decoded.slice(separator + 1))
622 } as any
623 })
624 }
625 }
626}

Callers 1

makeSecurityMiddlewareFunction · 0.85

Calls 4

sliceMethod · 0.80
makeMethod · 0.65
mapMethod · 0.45

Tested by

no test coverage detected