Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@macfja/sveltekit-cas

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@macfja/sveltekit-cas

CAS/SSO integration in SvelteKit

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

SvelteKit CAS authentication

A set of functions to ease usage of a CAS/SSO in SvelteKit

Installation

npm install --save @macfja/sveltekit-cas

Usage

Protect all pages that start with /profile/ and only allow user to go on his own page (/profile/my-cool-username) and fill the session with the token (or null) to provide to endpoints and the username of the connected user (or null)

// src/hooks.js
import { casHandler, getSessionToken, getSessionUser } from "@macfja/sveltekit-cas"

export async function handle({ request, resolve }) {
	return (
		(await casHandler(
			(request) => request.path.startsWith("/profile/"),
			(request, user) => {
				const regexp = request.path.match(/\/profile\/(\w+)/)
				return user !== regexp[1]
			},
			request
		)) || resolve(request)
	)
}

export function getSession(request) {
	return {
		...getSessionToken(request),
		...getSessionUser(request)
	}
}

Protect endpoint, so only connected user can access it

// src/routes/api/user.js
import { validate, validateUser } from "@macfja/sveltekit-cas"

export async function post({ headers }: ServerRequest): Promise<EndpointOutput> {
	const token = headers.token ?? null
	const access = validateUser(token, "admin")

	if (access !== null) {
		return access
	}

	// ... Do operation that only the user `admin` can do
}

export async function get({ headers }: ServerRequest): Promise<EndpointOutput> {
	const token = headers.token ?? null
	const access = validate(token)

	if (access !== null) {
		return access
	}

	// ... Do operation that only connected user can do
}

Configuration

The package have several configuration.
They are all have to be set as environment variables

NameDefaultComment
PUBLIC_HOSTno default, value is requiredPublic server domain name
PUBLIC_PORT443Public server port
CAS_HOSTno default, value is requiredHost of the SSO server
CAS_PORT443Port of the SSO server
CAS_SESSION_COOKIEsessionThe name of the cookie that will contain the JWT session
JWT_ISSsveltekit-casThe issuer of the JWT token
JWT_SECRETchangemeThe key used to generate the token signature

Contributing

Contributions are welcome. Please open up an issue or create PR if you would like to help out.

Read more in the Contributing file

License

The MIT License (MIT). Please see License File for more information.

Keywords

FAQs

Package last updated on 05 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc