Comparing version 0.9.5 to 0.10.0
@@ -11,15 +11,2 @@ export interface CookieAttributes { | ||
export declare function serializeCookie(name: string, value: string, attributes?: CookieAttributes): string; | ||
export declare function parseCookieHeader(header: string | null | undefined): Cookies; | ||
export declare class Cookies { | ||
constructor(cookies: Record<string, string> | Array<[string, string]>); | ||
private internal; | ||
get(cookieName: string): string | null; | ||
entries(): IterableIterator<[string, string]>; | ||
} | ||
export declare class Cookie { | ||
constructor(name: string, value: string, attributes: CookieAttributes); | ||
name: string; | ||
value: string; | ||
attributes: CookieAttributes; | ||
serialize(): string; | ||
} | ||
export declare function parseCookieHeader(header: string | null | undefined): Map<string, string>; |
@@ -34,3 +34,3 @@ export function serializeCookie(name, value, attributes = {}) { | ||
export function parseCookieHeader(header) { | ||
const cookieEntries = []; | ||
const cookies = new Map(); | ||
const items = header?.split("; ") ?? []; | ||
@@ -43,34 +43,5 @@ for (const item of items) { | ||
continue; | ||
cookieEntries.push([decodeURIComponent(rawKey), decodeURIComponent(rawValue)]); | ||
cookies.set(decodeURIComponent(rawKey), decodeURIComponent(rawValue)); | ||
} | ||
const cookies = new Cookies(cookieEntries); | ||
return cookies; | ||
} | ||
export class Cookies { | ||
constructor(cookies) { | ||
const entries = Array.isArray(cookies) ? cookies : Object.entries(cookies); | ||
for (const [name, value] of entries) { | ||
this.internal.set(name, value); | ||
} | ||
} | ||
internal = new Map(); | ||
get(cookieName) { | ||
return this.internal.get(cookieName) ?? null; | ||
} | ||
entries() { | ||
return this.internal.entries(); | ||
} | ||
} | ||
export class Cookie { | ||
constructor(name, value, attributes) { | ||
this.name = name; | ||
this.value = value; | ||
this.attributes = attributes; | ||
} | ||
name; | ||
value; | ||
attributes; | ||
serialize() { | ||
return serializeCookie(this.name, this.value, this.attributes); | ||
} | ||
} |
import { encodeBase64, encodeBase64url } from "../encoding/index.js"; | ||
import { authorizationHeader, createURL } from "./request.js"; | ||
import { authorizationHeader } from "./request.js"; | ||
export class OAuth2Controller { | ||
@@ -16,9 +16,14 @@ clientId; | ||
const scope = options?.scope ?? []; | ||
const authorizationUrl = createURL(this.authorizeEndpoint, { | ||
response_type: "code", | ||
client_id: this.clientId, | ||
scope: scope.join(" "), | ||
state: options?.state, | ||
redirect_uri: this.redirectURI | ||
}); | ||
const authorizationUrl = new URL(this.authorizeEndpoint); | ||
authorizationUrl.searchParams.set("response_type", "code"); | ||
authorizationUrl.searchParams.set("client_id", this.clientId); | ||
if (scope.length > 0) { | ||
authorizationUrl.searchParams.set("scope", scope.join(" ")); | ||
} | ||
if (options?.state !== undefined) { | ||
authorizationUrl.searchParams.set("state", options.state); | ||
} | ||
if (this.redirectURI) { | ||
authorizationUrl.searchParams.set("redirect_uri", this.redirectURI); | ||
} | ||
if (options?.codeVerifier !== undefined) { | ||
@@ -33,9 +38,12 @@ const codeChallengeBuffer = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(options.codeVerifier)); | ||
async validateAuthorizationCode(authorizationCode, options) { | ||
const body = createURLSearchParams({ | ||
code: authorizationCode, | ||
client_id: this.clientId, | ||
grant_type: "authorization_code", | ||
redirect_uri: this.redirectURI, | ||
code_verifier: options?.codeVerifier | ||
}); | ||
const body = new URLSearchParams(); | ||
body.set("code", authorizationCode); | ||
body.set("client_id", this.clientId); | ||
body.set("grant_type", "authorization_code"); | ||
if (this.redirectURI !== null) { | ||
body.set("redirect_uri", this.redirectURI); | ||
} | ||
if (options?.codeVerifier !== undefined) { | ||
body.set("code_verifier", options.codeVerifier); | ||
} | ||
const headers = new Headers({ | ||
@@ -62,3 +70,3 @@ "Content-Type": "application/x-www-form-urlencoded", | ||
const result = await response.json(); | ||
// github returns status 200 for errors | ||
// github returns status 200 for some errors | ||
if (!("access_token" in result) && "error" in result) { | ||
@@ -88,12 +96,2 @@ throw new AccessTokenRequestError(request, result); | ||
} | ||
function createURLSearchParams(params) { | ||
const base = new URLSearchParams(); | ||
for (const [key, value] of Object.entries(params)) { | ||
if (value === null || value === undefined) { | ||
continue; | ||
} | ||
base.set(key, value.toString()); | ||
} | ||
return base; | ||
} | ||
export class AccessTokenRequestError extends Error { | ||
@@ -100,0 +98,0 @@ request; |
@@ -1,2 +0,1 @@ | ||
export declare function createURL(url: string | URL, urlSearchParams: Record<string, string | number | undefined | null>): URL; | ||
export declare function authorizationHeader(type: "bearer" | "basic", token: string): string; |
@@ -1,10 +0,1 @@ | ||
export function createURL(url, urlSearchParams) { | ||
const newUrl = new URL(url); | ||
for (const [key, value] of Object.entries(urlSearchParams)) { | ||
if (value === null || value === undefined) | ||
continue; | ||
newUrl.searchParams.set(key, value.toString()); | ||
} | ||
return newUrl; | ||
} | ||
export function authorizationHeader(type, token) { | ||
@@ -11,0 +2,0 @@ if (type === "basic") { |
import { TimeSpan } from "../index.js"; | ||
import { Cookie } from "../cookie/index.js"; | ||
import type { CookieAttributes } from "../cookie/index.js"; | ||
export interface Session { | ||
@@ -38,7 +38,14 @@ sessionId: string; | ||
private baseCookieAttributes; | ||
createSessionCookie(sessionId: string): Cookie; | ||
createSessionCookie(sessionId: string): SessionCookie; | ||
/**Creates a new `Cookie` that deletes the existing cookie when set. */ | ||
createBlankSessionCookie(): Cookie; | ||
createBlankSessionCookie(): SessionCookie; | ||
parseCookieHeader(header: string | null | undefined): string | null; | ||
} | ||
export declare class SessionCookie { | ||
constructor(name: string, value: string, attributes: CookieAttributes); | ||
name: string; | ||
value: string; | ||
attributes: CookieAttributes; | ||
serialize(): string; | ||
} | ||
export {}; |
import { expirationDate, isWithinExpirationDate, TimeSpan } from "../index.js"; | ||
import { parseCookieHeader, Cookie } from "../cookie/index.js"; | ||
import { parseCookieHeader, serializeCookie } from "../cookie/index.js"; | ||
export class SessionController { | ||
@@ -68,3 +68,3 @@ /** | ||
createSessionCookie(sessionId) { | ||
return new Cookie(this.cookieName, sessionId, { | ||
return new SessionCookie(this.cookieName, sessionId, { | ||
...this.baseCookieAttributes, | ||
@@ -76,3 +76,3 @@ maxAge: this.sessionExpiresIn.seconds() | ||
createBlankSessionCookie() { | ||
return new Cookie(this.cookieName, "", { | ||
return new SessionCookie(this.cookieName, "", { | ||
...this.baseCookieAttributes, | ||
@@ -87,1 +87,14 @@ maxAge: 0 | ||
} | ||
export class SessionCookie { | ||
constructor(name, value, attributes) { | ||
this.name = name; | ||
this.value = value; | ||
this.attributes = attributes; | ||
} | ||
name; | ||
value; | ||
attributes; | ||
serialize() { | ||
return serializeCookie(this.name, this.value, this.attributes); | ||
} | ||
} |
{ | ||
"name": "oslo", | ||
"type": "module", | ||
"version": "0.9.5", | ||
"version": "0.10.0", | ||
"description": "A collection of auth-related utilities", | ||
@@ -6,0 +6,0 @@ "main": "dist/index.js", |
@@ -52,5 +52,4 @@ # `oslo` | ||
// returns Map<string, string> | ||
const cookies = parseCookieHeader("cookie1=hello; cookie2=bye"); | ||
const cookie1 = cookies.get("cookie1"); // string | null | ||
const entries = cookies.entries(); | ||
``` | ||
@@ -57,0 +56,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
72812
1539
480