GithubHelp home page GithubHelp logo

derek-zl / flask-cas Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ravomavain/flask-cas

0.0 2.0 0.0 87 KB

Flask extension for CAS

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

flask-cas's Introduction

Flask-CAS

Join the chat at https://gitter.im/cameronbwhite/Flask-CAS

Build Status

Flask-CAS is a Flask extension which makes it easy to authenticate with a CAS Server (v2.0+).

What is CAS?

The Central Authentication Service (CAS) is a single sign-on protocol for the web. Its purpose is to permit a user to access multiple applications while providing their credentials (such as userid and password) only once. It also allows web applications to authenticate users without gaining access to a user's security credentials, such as a password. The name CAS also refers to a software package that implements this protocol.

http://jasig.github.io/cas

Demo

Want to see it in action? Here is a live demo that lets you authenticate against your favorite CAS Server!

http://flask-cas-extension-demo.cameronbwhite.com/

Installation

PyPI

Flask-CAS is available on PyPI! You can install it with pip.

pip install Flask-CAS

Manual

If you want to do it the hard way you can clone the repository and install Flask-CAS in a virtualenv.

  1. Clone it git clone [email protected]:cameronbwhite/Flask-CAS.git
  2. Enter it cd Flask-CAS
  3. Create a virtualenv and enter it (Optional) virtualenv venv && source venv/bin/activate
  4. Install it python setup.py install

Instructions

After Flask-CAS is installed you will be able to import the flask.ext.cas packages. There is only one thing you care about inside the package which is the CAS class.

from flask.ext.cas import CAS

There are two ways to use the CAS class.

  1. Add the application object at construction time
app = Flask(__name__)
CAS(app)
  1. Or initialize the application with CAS.init_app
cas = CAS()
app = Flask(__name__)
cas.init_app(app)

The CAS class will add two routes /login/ and /logout/. You can prefix these routes if you pass a second argument to the CAS constructor or init_app depending on the method you choose.

The /login/ route will redirect the user to the CAS specified by the CAS_SERVER configuration value. If login is successful the user will be redirect to the endpoint specified by the CAS_AFTER_LOGIN configuration value, and the logged in user's username will be store in the session under the key specified by the CAS_USERNAME_SESSION_KEY configuration value. If attributes are available, they will be stored in the session under the key specified by the CAS_ATTRIBUTES_SESSION_KEY

The /logout/ route will redirect the user to the CAS logout page and the username and attributes will be removed from the session.

For convenience you can use the cas.login and cas.logout functions to redirect users to the login and logout pages.

from flask.ext.cas import login
from flask.ext.cas import logout

If you would like to require that a user is logged in before continuing you may use the cas.login_required method.

from flask.ext.cas import login_required

app.route('/foo')
@login_required
def foo():
    pass

Configuration

Required Configs

Key Description Example
CAS_SERVER URL of CAS 'http://sso.pdx.edu'
CAS_AFTER_LOGIN Endpoint to go to after successful login 'root'

Optional Configs

Key Default
CAS_TOKEN_SESSION_KEY _CAS_TOKEN
CAS_USERNAME_SESSION_KEY CAS_USERNAME
CAS_ATTRIBUTES_SESSION_KEY CAS_ATTRIBUTES
CAS_LOGIN_ROUTE '/cas'
CAS_LOGOUT_ROUTE '/cas/logout'
CAS_VALIDATE_ROUTE '/cas/serviceValidate'
CAS_AFTER_LOGOUT None

Example

import flask
from flask import Flask
from flask.ext.cas import CAS
from flask.ext.cas import login_required

app = Flask(__name__)
cas = CAS(app, '/cas')
app.config['CAS_SERVER'] = 'https://sso.pdx.edu' 
app.config['CAS_AFTER_LOGIN'] = 'route_root'

@app.route('/')
@login_required
def route_root():
    return flask.render_template(
        'layout.html',
        username = cas.username,
        display_name = cas.attributes['cas:displayName']
    )

flask-cas's People

Contributors

akameron avatar avacariu avatar gitter-badger avatar ravomavain avatar rbukharov avatar saipanno avatar warrn avatar xcompass avatar

Watchers

 avatar  avatar

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.