Giter Site home page Giter Site logo

sveltekit-cas's Introduction

SvelteKit CAS authentication

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

Github CI GitHub Repo stars NPM bundle size Download per week License NPM version Snyk Vulnerabilities for npm package

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)

// src/hooks.server.ts
import { sessionHook } from "@macfja/sveltekit-session"
import { casHandler } from "@macfja/sveltekit-cas"
import type { Handle } from "@sveltejs/kit"
import { sequence } from "@sveltejs/kit/hooks"

export const handle: Handle = sequence(
	sessionHook(),
	casHandler(
		"http://0.0.0.0:8080",
		2,
		(event) => event.url.pathname.startsWith("/profile/"),
		(event, user) => {
			const regexp = event.url.pathname.match(/\/profile\/(\w+)/)
			return user !== regexp[1]
		}
	)
)

Protect endpoint, so only connected user can access it

// src/routes/api/user/server.ts
import { error } from "@sveltejs/kit"
import { getUsername } from "@macfja/sveltekit-cas"
import type { RequestHandler } from "./$types"

export const POST: RequestHandler = async (event) => {
	if (getUsername(event) !== "admin") {
		throw error(403)
	}

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

export const GET: RequestHandler = async (event) => {
	if (getUsername(event) === undefined) {
		throw error(401)
	}

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

Configuration

The casHandler function take 4 parameters to change its behavior:

  • casRoot: The root URL to the CAS server
  • casVersion: The version of the CAS server (supported version: 1, 2, 3)
  • authRequired (optional): A function to indicate if a request should have an authenticated user
  • rejectAccess (optional): A function to indicate if a particular authenticated user if allowed to do a request

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.

sveltekit-cas's People

Contributors

macfja avatar

Stargazers

Hadouin avatar Hans Meiser avatar

Watchers

 avatar  avatar

sveltekit-cas's Issues

Support latest Sveltekit version

Using this module throws a few errors on the latest svelte version:
request has been replaced with event - your example hooks.js
event.headers has been replaced with event.request.headers
event.path has been replaced with event.url.pathname and so on

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.