Skip to content

Commit

Permalink
Replace Oslo (#1708)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper authored Oct 7, 2024
1 parent 46ebc6a commit e813f32
Show file tree
Hide file tree
Showing 25 changed files with 450 additions and 67 deletions.
6 changes: 6 additions & 0 deletions .auri/$rwuelhob.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
package: "@lucia-auth/adapter-test" # package name
type: "patch" # "major", "minor", "patch"
---

Update dependencies.
6 changes: 6 additions & 0 deletions .auri/$ymtw2zct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
package: "lucia" # package name
type: "patch" # "major", "minor", "patch"
---

Update dependencies.
2 changes: 1 addition & 1 deletion docs/pages/basics/sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ See the [Validate session cookies](/guides/validate-session-cookies) and [Valida

### Create session cookies

You can create a session cookie for a session with [`Lucia.createSessionCookie()`](/reference/main/Lucia/createSessionCookie). It takes a session and returns a new [`Cookie`](/reference/main/Cookie) instance. You can either use [`Cookie.serialize()`](https://oslo.js.org/reference/cookie/Cookie/serialize) to create `Set-Cookie` HTTP header value, or use your framework's API by accessing the name, value, and session.
You can create a session cookie for a session with [`Lucia.createSessionCookie()`](/reference/main/Lucia/createSessionCookie). It takes a session and returns a new [`Cookie`](/reference/main/Cookie) instance. You can either use [`Cookie.serialize()`](/reference/main/Cookie) to create `Set-Cookie` HTTP header value, or use your framework's API by accessing the name, value, and session.

```ts
const sessionCookie = lucia.createSessionCookie(session.id);
Expand Down
6 changes: 1 addition & 5 deletions docs/pages/basics/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@ await db.createUser({
});
```

Use Lucia's [`generateId()`](/reference/main/generateIdFromEntropySize) or Oslo's [`generateRandomString()`](https://oslo.js.org/reference/crypto/generateRandomString) if you're looking for a more customizable option.
Use Lucia's [`generateId()`](/reference/main/generateIdFromEntropySize).

```ts
import { generateId } from "lucia";

const id = generateId(15);

import { generateRandomString, alphabet } from "oslo/crypto";

const id = generateRandomString(15, alphabet("a-z", "A-Z", "0-9"));
```

## Define user attributes
Expand Down
7 changes: 0 additions & 7 deletions docs/pages/reference/main/Cookie.md

This file was deleted.

67 changes: 67 additions & 0 deletions docs/pages/reference/main/Cookie/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
title: "Cookie"
---

# `Cookie`

Represents a cookie.

## Constructor

```ts
//$ CookieAttributes=/reference/main/CookieAttributes
function constructor(name: string, value: string, attributes?: $$CookieAttributes): this;
```

### Parameters

- `name`
- `value`
- `attributes`

## Methods

- [`serialize()`](/reference/main/Cookie/serialize)

## Properties

```ts
//$ CookieAttributes=/reference/main/CookieAttributes
interface Properties {
name: string;
value: string;
attributes: $$CookieAttributes;
}
```

- `name`
- `value`
- `attributes`

## Example

```ts
import { Cookie } from "lucia";

const sessionCookie = new Cookie("session", sessionId, {
maxAge: 60 * 60 * 24,
httpOnly: true,
secure: true,
path: "/"
});
response.headers.set("Set-Cookie", sessionCookie.serialize());
```

If your framework provides an API for setting cookies:

```ts
import { Cookie } from "lucia";

const sessionCookie = new Cookie("session", sessionId, {
maxAge: 60 * 60 * 24,
httpOnly: true,
secure: true,
path: "/"
});
setCookie(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
```
17 changes: 17 additions & 0 deletions docs/pages/reference/main/Cookie/serialize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: "Cookie.serialize()"
---

# `Cookie.serialize()`

Serializes cookie for `Set-Cookie` header.

```ts
function serialize(): string;
```

## Example

```ts
response.headers.set("Set-Cookie", cookie.serialize());
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ Method of [`Lucia`](/reference/main/Lucia). Creates a new cookie with a blank va
## Definition

```ts
//$ Cookie=/reference/cookie/Cookie
//$ Cookie=/reference/main/Cookie
function createBlankSessionCookie(): $$Cookie;
```
2 changes: 1 addition & 1 deletion docs/pages/reference/main/Lucia/createSessionCookie.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ Method of [`Lucia`](/reference/main/Lucia). Creates a new session cookie.
## Definition

```ts
//$ Cookie=/reference/cookie/Cookie
//$ Cookie=/reference/main/Cookie
function createSessionCookie(sessionId: string): $$Cookie;
```
2 changes: 1 addition & 1 deletion docs/pages/reference/main/Scrypt/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A pure JS implementation of Scrypt. Provides methods for hashing passwords and v

The output hash is a combination of the scrypt hash and the 32-bytes salt, in the format of `<salt>:<hash>`.

Since it's pure JS, it is anywhere from 2~3 times slower than implementations based on native code. See Oslo's [`Scrypt`](https://oslo.js.org/reference/password/Scrypt) for a faster API (Node.js-only).
Since it's pure JS, it is anywhere from 2~3 times slower than implementations based on native code.

## Constructor

Expand Down
7 changes: 0 additions & 7 deletions docs/pages/reference/main/TimeSpan.md

This file was deleted.

50 changes: 50 additions & 0 deletions docs/pages/reference/main/TimeSpan/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: "TimeSpan"
---

# `TimeSpan`

Represents a time-span. Supports negative values.

## Constructor

```ts
//$ TimeSpanUnit=/reference/main/TimeSpanUnit
function constructor(value: number, unit: $$TimeSpanUnit): this;
```

### Parameters

- `value`
- `unit`: `ms` for milliseconds, `s` for seconds, etc

## Methods

- [`milliseconds()`](/reference/main/TimeSpan/milliseconds)
- [`seconds()`](/reference/main/TimeSpan/seconds)

## Properties

```ts
//$ TimeSpanUnit=/reference/main/TimeSpanUnit
interface Properties {
unit: $$TimeSpanUnit;
value: number;
}
```

- `unit`
- `value`

## Example

```ts
import { TimeSpan } from "oslo";

const halfSeconds = new TimeSpan(500, "ms");
const tenSeconds = new TimeSpan(10, "s");
const halfHour = new TimeSpan(30, "m");
const oneHour = new TimeSpan(1, "h");
const oneDay = new TimeSpan(1, "d");
const oneWeek = new TimeSpan(1, "w");
```
20 changes: 20 additions & 0 deletions docs/pages/reference/main/TimeSpan/milliseconds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: "Timespan.milliseconds()"
---

# `Timespan.milliseconds()`

Returns the time-span in milliseconds.

## Definition

```ts
function milliseconds(): number;
```

## Example

```ts
// 60 * 1000 = 60,000 ms
new TimeSpan("60", "s").milliseconds();
```
20 changes: 20 additions & 0 deletions docs/pages/reference/main/TimeSpan/seconds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: "Timespan.seconds()"
---

# `Timespan.seconds()`

Returns the time-span in seconds.

## Definition

```ts
function seconds(): number;
```

## Example

```ts
// 60 * 60 = 3600 s
new TimeSpan(1, "h").seconds();
```
29 changes: 27 additions & 2 deletions docs/pages/reference/main/verifyRequestOrigin.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@
title: "verifyRequestOrigin()"
---

# `Cookie`
# `verifyRequestOrigin()`

See [`verifyRequestOrigin()`](https://oslo.js.org/reference/request/verifyRequestOrigin) from `oslo/request`.
Verifies the request originates from a trusted origin by comparing the `Origin` header and host (e.g. `Host` header).

## Definition

```ts
function verifyRequestOrigin(origin: string, allowedDomains: string[]): boolean;
```

### Parameters

- `origin`: `Origin` header
- `allowedDomains`: Allowed request origins, full URL or URL host

## Example

```ts
import { verifyRequestOrigin } from "lucia";

// true
verifyRequestOrigin("https://example.com", ["example.com"]);
verifyRequestOrigin("https://example.com", ["https://example.com"]);

// false
verifyRequestOrigin("https://foo.example.com", ["example.com"]);
verifyRequestOrigin("https://example.com", ["foo.example.com"]);
```
11 changes: 5 additions & 6 deletions packages/adapter-test/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { Adapter, DatabaseSession, DatabaseUser } from "lucia";
import { generateRandomString, alphabet } from "oslo/crypto";
import { Adapter, DatabaseSession, DatabaseUser, generateId } from "lucia";
import assert from "node:assert/strict";

export const databaseUser: DatabaseUser = {
id: generateRandomString(15, alphabet("0-9", "a-z")),
id: generateId(15),
attributes: {
username: generateRandomString(15, alphabet("0-9", "a-z"))
username: generateId(15)
}
};

export async function testAdapter(adapter: Adapter) {
console.log(`\n\x1B[38;5;63;1m[start] \x1B[0mRunning adapter tests\x1B[0m\n`);
const databaseSession: DatabaseSession = {
userId: databaseUser.id,
id: generateRandomString(40, alphabet("0-9", "a-z")),
id: generateId(40),
// get random date with 0ms
expiresAt: new Date(Math.floor(Date.now() / 1000) * 1000 + 10_000),
attributes: {
Expand Down Expand Up @@ -54,7 +53,7 @@ export async function testAdapter(adapter: Adapter) {
await test("deleteExpiredSessions() deletes all expired sessions", async () => {
const expiredSession: DatabaseSession = {
userId: databaseUser.id,
id: generateRandomString(40, alphabet("0-9", "a-z")),
id: generateId(40),
expiresAt: new Date(Math.floor(Date.now() / 1000) * 1000 - 10_000),
attributes: {
country: "us"
Expand Down
3 changes: 2 additions & 1 deletion packages/lucia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"vitest": "^0.33.0"
},
"dependencies": {
"oslo": "1.2.0"
"@oslojs/crypto": "^1.0.1",
"@oslojs/encoding": "^1.1.0"
}
}
Loading

0 comments on commit e813f32

Please sign in to comment.