Compare commits

..

9 Commits

Author SHA1 Message Date
GitHub Actions
e459d2d7e2 chore(release): bump package version(s) [skip ci] 2023-07-18 14:40:11 +00:00
Thang Vu
db1fd9007c fix(ts): types in sveltekit 2023-07-18 21:29:04 +07:00
Thang Vu
0439fc5fc6 feat(providers): add request param to sendVerificationRequest (#8071)
Co-authored-by: Corey Jepperson <11298888+acoreyj@users.noreply.github.com>
2023-07-18 15:39:11 +02:00
Benjamin Tamasi
d0dd2ababc fix(sveltekit): prefix for getSession url (#6478)
* [SvelteKit] fix getSession url

remove `/api` prefix from getSession function.

* Update packages/frameworks-sveltekit/src/lib/index.ts

---------

Co-authored-by: Thang Vu <hi@thvu.dev>
2023-07-16 21:01:25 +07:00
Thang Vu
ba58d48dba fix(providers): add authorization params for AzureAD (#8047)
https: //github.com/nextauthjs/next-auth/pull/5668

Co-authored-by: Andres Jose Sebastian Rincon Gonzalez <2531975+stianrincon@users.noreply.github.com>
2023-07-15 22:01:24 +07:00
Thang Vu
a8d76ed440 fix(ts): require id for updateUser param (#8044)
https: //github.com/nextauthjs/next-auth/pull/5431

Co-authored-by: Yuri Sulyma <453486+ysulyma@users.noreply.github.com>
2023-07-15 17:18:15 +07:00
Thang Vu
3d7b8720db chore(docs): OIDC example for BoxyHQ (#8032)
chore(docs): OIDC example for BoxyHQ

Co-authored-by: Deepak Prabhakara <deepak@boxyhq.com>
2023-07-13 23:43:10 +07:00
Francis Gulotta
1e886b97bc fix(EmailProvider): proper required fields and allow all nodemailer types (#8016) 2023-07-11 18:01:47 +02:00
Tal Aharoni
ecb14ccecd fix: correct Descope provider config (#8003) 2023-07-11 12:51:32 +02:00
15 changed files with 157 additions and 74 deletions

View File

@@ -112,6 +112,7 @@ providers: [
identifier: email,
url,
provider: { server, from },
request // for example can be used to get the user agent (`request.headers.get("user-agent")`) to parse and pass on to the user in the email so they can be more confident they originated the request
}) {
/* your function */
},

View File

@@ -1,7 +1,7 @@
{
"name": "@auth/dynamodb-adapter",
"repository": "https://github.com/nextauthjs/next-auth",
"version": "1.0.0",
"version": "1.0.1",
"description": "AWS DynamoDB adapter for next-auth.",
"keywords": [
"next-auth",

View File

@@ -265,9 +265,8 @@ export function DynamoDBAdapter(
const data = await client.update({
TableName,
Key: {
// next-auth type is incorrect it should be Partial<AdapterUser> & {id: string} instead of just Partial<AdapterUser>
[pk]: `USER#${user.id as string}`,
[sk]: `USER#${user.id as string}`,
[pk]: `USER#${user.id}`,
[sk]: `USER#${user.id}`,
},
UpdateExpression,
ExpressionAttributeNames,

View File

@@ -1,6 +1,6 @@
{
"name": "@auth/core",
"version": "0.9.0",
"version": "0.10.0",
"description": "Authentication for the Web.",
"keywords": [
"authentication",

View File

@@ -223,7 +223,7 @@ export interface Adapter {
getUserByAccount?(
providerAccountId: Pick<AdapterAccount, "provider" | "providerAccountId">
): Awaitable<AdapterUser | null>
updateUser?(user: Partial<AdapterUser>): Awaitable<AdapterUser>
updateUser?(user: Partial<AdapterUser> & Pick<AdapterUser, 'id'>): Awaitable<AdapterUser>
/** @todo This method is currently not invoked yet. */
deleteUser?(
userId: string

View File

@@ -1,13 +1,14 @@
import { createHash, randomString } from "../web.js"
import { createHash, randomString, toRequest } from "../web.js"
import type { InternalOptions } from "../../types.js"
import type { InternalOptions, RequestInternal } from "../../types.js"
/**
* Starts an e-mail login flow, by generating a token,
* and sending it to the user's e-mail (with the help of a DB adapter)
*/
export default async function email(
identifier: string,
options: InternalOptions<"email">
options: InternalOptions<"email">,
request: RequestInternal
): Promise<string> {
const { url, adapter, provider, callbackUrl, theme } = options
const token =
@@ -31,6 +32,7 @@ export default async function email(
url: _url,
provider,
theme,
request: toRequest(request),
}),
// @ts-expect-error -- Verified in `assertConfig`.
adapter.createVerificationToken?.({

View File

@@ -137,8 +137,7 @@ export async function AuthInternal<
case "signin":
if ((csrfDisabled || options.csrfTokenVerified) && options.provider) {
const signin = await routes.signin(
request.query,
request.body,
request,
options
)
if (signin.cookies) cookies.push(...signin.cookies)

View File

@@ -16,10 +16,10 @@ import type {
* For Email, sends an email with a sign in link.
*/
export async function signin(
query: RequestInternal["query"],
body: RequestInternal["body"],
request: RequestInternal,
options: InternalOptions<"oauth" | "oidc" | "email">
): Promise<ResponseInternal> {
const { query, body } = request
const { url, logger, provider } = options
try {
if (provider.type === "oauth" || provider.type === "oidc") {
@@ -48,7 +48,7 @@ export async function signin(
if (unauthorizedOrError) return unauthorizedOrError
const redirect = await emailSignin(email, options)
const redirect = await emailSignin(email, options, request)
return { redirect }
}
return { redirect: `${url}/signin` }

View File

@@ -72,6 +72,17 @@ export async function toInternalRequest(
}
}
export function toRequest(request: RequestInternal): Request {
return new Request(request.url, {
headers: request.headers,
method: request.method,
body:
request.method === "POST"
? JSON.stringify(request.body ?? {})
: undefined,
})
}
export function toResponse(res: ResponseInternal): Response {
const headers = new Headers(res.headers)

View File

@@ -131,6 +131,11 @@ export default function AzureAD<P extends AzureADProfile>(
name: "Azure Active Directory",
type: "oidc",
wellKnown: `${rest.issuer}}/.well-known/openid-configuration?appid=${options.clientId}`,
authorization: {
params: {
scope: 'openid profile email User.Read',
},
},
async profile(profile, tokens) {
// https://docs.microsoft.com/en-us/graph/api/profilephoto-get?view=graph-rest-1.0#examples
const response = await fetch(

View File

@@ -20,7 +20,7 @@ export interface BoxyHQSAMLProfile extends Record<string, any> {
/**
* Add BoxyHQ SAML login to your page.
*
* BoxyHQ SAML is an open source service that handles the SAML login flow as an OAuth 2.0 flow, abstracting away all the complexities of the SAML protocol.
* BoxyHQ SAML is an open source service that handles the SAML SSO login flow as an OAuth 2.0 flow, abstracting away all the complexities of the SAML protocol. Enable Enterprise single-sign-on in your app with ease.
*
* You can deploy BoxyHQ SAML as a separate service or embed it into your app using our NPM library. [Check out the documentation for more details](https://boxyhq.com/docs/jackson/deploy)
*
@@ -32,13 +32,38 @@ export interface BoxyHQSAMLProfile extends Record<string, any> {
* ```
*
* #### Configuration
*
* For OAuth 2.0 Flow:
*```js
* import Auth from "@auth/core"
* import BoxyHQ from "@auth/core/providers/boxyhq-saml"
*
* const request = new Request(origin)
* const response = await Auth(request, {
* providers: [BoxyHQ({ clientId: BOXYHQ_SAML_CLIENT_ID, clientSecret: BOXYHQ_SAML_CLIENT_SECRET. issuer: BOXYHQ_SAML_ISSUER })],
* providers: [BoxyHQ({
* authorization: { params: { scope: "" } }, // This is needed for OAuth 2.0 flow, otherwise default to openid
* clientId: BOXYHQ_SAML_CLIENT_ID,
* clientSecret: BOXYHQ_SAML_CLIENT_SECRET,
* issuer: BOXYHQ_SAML_ISSUER
* })],
* })
* ```
* For OIDC Flow:
*
*```js
* import Auth from "@auth/core"
* import BoxyHQ from "@auth/core/providers/boxyhq-saml"
*
* const request = new Request(origin)
* const response = await Auth(request, {
* providers: [BoxyHQ({
* id: "boxyhq-saml-oidc",
* wellKnown: `http://localhost:5225/.well-known/openid-configuration`,
* authorization: { params: { scope: "openid email" } },
* clientId: BOXYHQ_SAML_CLIENT_ID,
* clientSecret: BOXYHQ_SAML_CLIENT_SECRET,
* issuer: BOXYHQ_SAML_ISSUER
* })],
* })
* ```
*

View File

@@ -13,17 +13,25 @@
import type { OIDCConfig, OIDCUserConfig } from "./index.js"
/** The returned user profile from Descope when using the profile callback. */
/** The returned user profile from Descope when using the profile callback.
* [See Load User](https://docs.descope.com/api/openapi/usermanagement/operation/LoadUser/)
*/
export interface DescopeProfile {
/** The user Descope ID */
/** The user's unique Descope ID */
sub: string
/** The user's name */
name: string
/** The user's email */
email: string
/** A boolean indicating if the user's email is verified */
email_verified: boolean
/** The user's phone number */
phone_number: string
/** A boolean indicating if the user's phone number is verified */
phone_number_verified: boolean
/** The user's picture */
picture: string
/** Custom user's attributes */
/** The user's custom attributes */
[claim: string]: unknown
}
@@ -97,7 +105,7 @@ export default function Descope(
id: "descope",
name: "Descope",
type: "oidc",
clientId: `https://api.descope.com/${config.clientId}`,
issuer: `https://api.descope.com/${config.clientId}`,
style: {
logo: "/descope.svg",
logoDark: "/descope.svg",

View File

@@ -1,9 +1,17 @@
import { createTransport } from "nodemailer"
import type { CommonProviderOptions } from "./index.js"
import type { Options as SMTPTransportOptions } from "nodemailer/lib/smtp-transport"
import type { Awaitable, Theme } from "../types.js"
import { Transport, TransportOptions, createTransport } from "nodemailer"
import * as JSONTransport from "nodemailer/lib/json-transport/index.js"
import * as SendmailTransport from "nodemailer/lib/sendmail-transport/index.js"
import * as SESTransport from "nodemailer/lib/ses-transport/index.js"
import * as SMTPTransport from "nodemailer/lib/smtp-transport/index.js"
import * as SMTPPool from "nodemailer/lib/smtp-pool/index.js"
import * as StreamTransport from "nodemailer/lib/stream-transport/index.js"
// TODO: Make use of https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html for the string
type AllTransportOptions = string | SMTPTransport | SMTPTransport.Options | SMTPPool | SMTPPool.Options | SendmailTransport | SendmailTransport.Options | StreamTransport | StreamTransport.Options | JSONTransport | JSONTransport.Options | SESTransport | SESTransport.Options | Transport<any> | TransportOptions
export interface SendVerificationRequestParams {
identifier: string
url: string
@@ -11,6 +19,7 @@ export interface SendVerificationRequestParams {
provider: EmailConfig
token: string
theme: Theme
request: Request
}
/**
@@ -26,10 +35,9 @@ export interface SendVerificationRequestParams {
*
* [Custom email service with Auth.js](https://authjs.dev/guides/providers/email#custom-email-service)
*/
export interface EmailConfig extends CommonProviderOptions {
type: "email"
// TODO: Make use of https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html
server?: string | SMTPTransportOptions
export interface EmailUserConfig {
server?: AllTransportOptions
type?: "email"
/** @default `"Auth.js <no-reply@authjs.dev>"` */
from?: string
/**
@@ -40,7 +48,7 @@ export interface EmailConfig extends CommonProviderOptions {
*/
maxAge?: number
/** [Documentation](https://authjs.dev/guides/providers/email#customizing-emails) */
sendVerificationRequest: (
sendVerificationRequest?: (
params: SendVerificationRequestParams
) => Awaitable<void>
/**
@@ -77,24 +85,49 @@ export interface EmailConfig extends CommonProviderOptions {
normalizeIdentifier?: (identifier: string) => string
}
export interface EmailConfig extends CommonProviderOptions {
// defaults
id: "email"
type: "email"
name: "Email"
server: AllTransportOptions
from: string
maxAge: number
sendVerificationRequest: (
params: SendVerificationRequestParams
) => Awaitable<void>
/**
* This is copied into EmailConfig in parseProviders() don't use elsewhere
*/
options: EmailUserConfig
// user options
// TODO figure out a better way than copying from EmailUserConfig
secret?: string
generateVerificationToken?: () => Awaitable<string>
normalizeIdentifier?: (identifier: string) => string
}
// TODO: Rename to Token provider
// when started working on https://github.com/nextauthjs/next-auth/discussions/1465
export type EmailProviderType = "email"
/**
/**
* ## Overview
* The Email provider uses email to send "magic links" that can be used to sign in, you will likely have seen these if you have used services like Slack before.
*
*
* Adding support for signing in via email in addition to one or more OAuth services provides a way for users to sign in if they lose access to their OAuth account (e.g. if it is locked or deleted).
*
*
* The Email provider can be used in conjunction with (or instead of) one or more OAuth providers.
* ### How it works
*
*
* On initial sign in, a **Verification Token** is sent to the email address provided. By default this token is valid for 24 hours. If the Verification Token is used within that time (i.e. by clicking on the link in the email) an account is created for the user and they are signed in.
*
*
*
*
* If someone provides the email address of an _existing account_ when signing in, an email is sent and they are signed into the account associated with that email address when they follow the link in the email.
*
*
* :::tip
* The Email Provider can be used with both JSON Web Tokens and database sessions, but you **must** configure a database to use it. It is not possible to enable email sign in without using a database.
* :::
@@ -103,20 +136,20 @@ export type EmailProviderType = "email"
* 1. NextAuth.js does not include `nodemailer` as a dependency, so you'll need to install it yourself if you want to use the Email Provider. Run `npm install nodemailer` or `yarn add nodemailer`.
* 2. You will need an SMTP account; ideally for one of the [services known to work with `nodemailer`](https://community.nodemailer.com/2-0-0-beta/setup-smtp/well-known-services/).
* 3. There are two ways to configure the SMTP server connection.
*
*
* You can either use a connection string or a `nodemailer` configuration object.
*
*
* 3.1 **Using a connection string**
*
*
* Create an `.env` file to the root of your project and add the connection string and email address.
*
*
* ```js title=".env" {1}
* EMAIL_SERVER=smtp://username:password@smtp.example.com:587
* EMAIL_FROM=noreply@example.com
* ```
*
*
* Now you can add the email provider like this:
*
*
* ```js {3} title="pages/api/auth/[...nextauth].js"
* import EmailProvider from "next-auth/providers/email";
* ...
@@ -127,11 +160,11 @@ export type EmailProviderType = "email"
* }),
* ],
* ```
*
*
* 3.2 **Using a configuration object**
*
*
* In your `.env` file in the root of your project simply add the configuration object options individually:
*
*
* ```js title=".env"
* EMAIL_SERVER_USER=username
* EMAIL_SERVER_PASSWORD=password
@@ -139,9 +172,9 @@ export type EmailProviderType = "email"
* EMAIL_SERVER_PORT=587
* EMAIL_FROM=noreply@example.com
* ```
*
*
* Now you can add the provider settings to the NextAuth.js options object in the Email Provider.
*
*
* ```js title="pages/api/auth/[...nextauth].js"
* import EmailProvider from "next-auth/providers/email";
* ...
@@ -159,19 +192,19 @@ export type EmailProviderType = "email"
* }),
* ],
* ```
*
*
* 4. Do not forget to setup one of the database [adapters](https://authjs.dev/reference/adapters) for storing the Email verification token.
*
*
* 5. You can now sign in with an email address at `/api/auth/signin`.
*
*
* A user account (i.e. an entry in the Users table) will not be created for the user until the first time they verify their email address. If an email address is already associated with an account, the user will be signed in to that account when they use the link in the email.
*
*
* ## Customizing emails
*
*
* You can fully customize the sign in email that is sent by passing a custom function as the `sendVerificationRequest` option to `EmailProvider()`.
*
*
* e.g.
*
*
* ```js {3} title="pages/api/auth/[...nextauth].js"
* import EmailProvider from "next-auth/providers/email";
* ...
@@ -189,12 +222,12 @@ export type EmailProviderType = "email"
* }),
* ]
* ```
*
*
* The following code shows the complete source for the built-in `sendVerificationRequest()` method:
*
*
* ```js
* import { createTransport } from "nodemailer"
*
*
* async function sendVerificationRequest(params) {
* const { identifier, url, provider, theme } = params
* const { host } = new URL(url)
@@ -212,12 +245,12 @@ export type EmailProviderType = "email"
* throw new Error(`Email(s) (${failed.join(", ")}) could not be sent`)
* }
* }
*
*
* function html(params: { url: string; host: string; theme: Theme }) {
* const { url, host, theme } = params
*
*
* const escapedHost = host.replace(/\./g, "&#8203;.")
*
*
* const brandColor = theme.brandColor || "#346df1"
* const color = {
* background: "#f9f9f9",
@@ -227,7 +260,7 @@ export type EmailProviderType = "email"
* buttonBorder: brandColor,
* buttonText: theme.buttonText || "#fff",
* }
*
*
* return `
* <body style="background: ${color.background};">
* <table width="100%" border="0" cellspacing="20" cellpadding="0"
@@ -260,21 +293,21 @@ export type EmailProviderType = "email"
* </body>
* `
* }
*
*
* // Email Text body (fallback for email clients that don't render HTML, e.g. feature phones)
* function text({ url, host }: { url: string; host: string }) {
* return `Sign in to ${host}\n${url}\n\n`
* }
* ```
*
*
* :::tip
* If you want to generate great looking email client compatible HTML with React, check out https://mjml.io
* :::
*
*
* ## Customizing the Verification Token
*
*
* By default, we are generating a random verification token. You can define a `generateVerificationToken` method in your provider options if you want to override it:
*
*
* ```js title="pages/api/auth/[...nextauth].js"
* providers: [
* EmailProvider({
@@ -284,9 +317,9 @@ export type EmailProviderType = "email"
* })
* ],
* ```
*
*
* ## Normalizing the email address
*
*
* By default, Auth.js will normalize the email address. It treats values as case-insensitive (which is technically not compliant to the [RFC 2821 spec](https://datatracker.ietf.org/doc/html/rfc2821), but in practice this causes more problems than it solves, eg. when looking up users by e-mail from databases.) and also removes any secondary email address that was passed in as a comma-separated list. You can apply your own normalization via the `normalizeIdentifier` method on the `EmailProvider`. The following example shows the default behavior:
* ```ts
* EmailProvider({
@@ -299,7 +332,7 @@ export type EmailProviderType = "email"
* // but we remove it on the domain part
* domain = domain.split(",")[0]
* return `${local}@${domain}`
*
*
* // You can also throw an error, which will redirect the user
* // to the sign-in page with error=EmailSignin in the URL
* // if (identifier.split("@").length > 2) {
@@ -308,12 +341,12 @@ export type EmailProviderType = "email"
* },
* })
* ```
*
*
* :::warning
* Always make sure this returns a single e-mail address, even if multiple ones were passed in.
* :::
*/
export default function Email(config: EmailConfig): EmailConfig {
export default function Email(config: EmailUserConfig): EmailConfig {
return {
id: "email",
type: "email",
@@ -337,7 +370,6 @@ export default function Email(config: EmailConfig): EmailConfig {
throw new Error(`Email (${failed.join(", ")}) could not be sent`)
}
},
// @ts-expect-error
options: config,
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "@auth/sveltekit",
"version": "0.3.5",
"version": "0.3.6",
"description": "Authentication for SvelteKit.",
"keywords": [
"authentication",

View File

@@ -211,12 +211,13 @@ import type { AuthAction, AuthConfig, Session } from "@auth/core/types"
export async function getSession(
req: Request,
config: AuthConfig
config: SvelteKitAuthConfig
): ReturnType<App.Locals["getSession"]> {
config.secret ??= env.AUTH_SECRET
config.trustHost ??= true
const url = new URL("/api/auth/session", req.url)
const prefix = config.prefix ?? "/auth"
const url = new URL(prefix + "/session", req.url)
const request = new Request(url, { headers: req.headers })
const response = await Auth(request, config)