Skip to content

Commit

Permalink
feat: Add support for secure attribute of local/refresh provider cook…
Browse files Browse the repository at this point in the history
…ies (#729)

* Fixed misplaced comment and added comment for the duration in human-readable time.

* Added secure cookie attribute for local and refresh provider.

* Set secure attribute of token to false by default.

* Added documentation for the added secureCookieAttribute.

* Update useAuthState.ts

---------

Co-authored-by: Marsel Shayhin <[email protected]>
  • Loading branch information
matteioo and phoenix-ru authored May 16, 2024
1 parent a863d92 commit f3fc581
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
28 changes: 22 additions & 6 deletions docs/content/2.configuration/2.nuxt-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,20 @@ type ProviderLocal = {
*/
sameSiteAttribute?: boolean | 'lax' | 'strict' | 'none' | undefined,
/**
* The cookie domain. See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
* Whether to set the secure flag on the cookie. This is useful when the application is served over HTTPS.
*
* @default false
* @example true
*/
secureCookieAttribute?: boolean,
/**
* The cookie domain.
* See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
*
* @default ''
* @example sidebase.io
* @example 'sidebase.io'
*/
cookieDomain?: string;
cookieDomain?: string,
},
/*
* Settings for the session-data that `nuxt-auth` receives from the `getSession` endpoint.
Expand Down Expand Up @@ -401,12 +409,20 @@ type ProviderRefresh = {
*/
sameSiteAttribute?: boolean | 'lax' | 'strict' | 'none' | undefined,
/**
* The cookie domain. See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
* Whether to set the secure flag on the cookie. This is useful when the application is served over HTTPS.
*
* @default false
* @example true
*/
secureCookieAttribute?: boolean,
/**
* The cookie domain.
* See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
*
* @default ''
* @example sidebase.io
* @example 'sidebase.io'
*/
cookieDomain?: string;
cookieDomain?: string,
},
/**
* Settings for the authentication-refreshToken that `nuxt-auth` receives from the `signIn` endpoint and that can be used to authenticate subsequent requests.
Expand Down
5 changes: 4 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ const defaultsByBackend: {
type: 'Bearer',
cookieName: 'auth.token',
headerName: 'Authorization',
maxAgeInSeconds: 30 * 60,
maxAgeInSeconds: 30 * 60, // 30 minutes
sameSiteAttribute: 'lax',
secureCookieAttribute: false,
cookieDomain: ''
},
session: {
Expand Down Expand Up @@ -86,13 +87,15 @@ const defaultsByBackend: {
headerName: 'Authorization',
maxAgeInSeconds: 5 * 60, // 5 minutes
sameSiteAttribute: 'none',
secureCookieAttribute: false,
cookieDomain: ''
},
refreshToken: {
signInResponseRefreshTokenPointer: '/refreshToken',
refreshRequestTokenPointer: '/refreshToken',
cookieName: 'auth.refresh-token',
maxAgeInSeconds: 60 * 60 * 24 * 7, // 7 days
secureCookieAttribute: false,
cookieDomain: ''
},
session: {
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/composables/local/useAuthState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export const useAuthState = (): UseAuthStateReturn => {
default: () => null,
domain: config.token.cookieDomain,
maxAge: config.token.maxAgeInSeconds,
sameSite: config.token.sameSiteAttribute
sameSite: config.token.sameSiteAttribute,
secure: config.token.secureCookieAttribute
})

const rawToken = useState('auth:raw-token', () => _rawTokenCookie.value)
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/composables/refresh/useAuthState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const useAuthState = (): UseAuthStateReturn => {
default: () => null,
domain: config.refreshToken.cookieDomain,
maxAge: config.refreshToken.maxAgeInSeconds,
sameSite: 'lax'
sameSite: 'lax',
secure: config.refreshToken.secureCookieAttribute
}
)

Expand Down
24 changes: 20 additions & 4 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,18 @@ export type ProviderLocal = {
*/
sameSiteAttribute?: boolean | 'lax' | 'strict' | 'none' | undefined;
/**
* The cookie domain. See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
* Whether to set the secure flag on the cookie. This is useful when the application is served over HTTPS.
*
* @default false
* @example true
*/
secureCookieAttribute?: boolean;
/**
* The cookie domain.
* See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
*
* @default ''
* @example sidebase.io
* @example 'sidebase.io'
*/
cookieDomain?: string;
};
Expand Down Expand Up @@ -270,10 +278,18 @@ export type ProviderLocalRefresh = Omit<ProviderLocal, 'type'> & {
*/
maxAgeInSeconds?: number;
/**
* The cookie domain. See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
* Whether to set the secure flag on the cookie. This is useful when the application is served over HTTPS.
*
* @default false
* @example true
*/
secureCookieAttribute?: boolean;
/**
* The cookie domain.
* See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
*
* @default ''
* @example sidebase.io
* @example 'sidebase.io'
*/
cookieDomain?: string;
};
Expand Down

0 comments on commit f3fc581

Please sign in to comment.