-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from keitaroinc/initial-implementation
Initial implementation
- Loading branch information
Showing
21 changed files
with
629 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,6 @@ coverage.xml | |
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# Saml2 config | ||
idp.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,16 @@ | ||
language: python | ||
sudo: required | ||
|
||
# use an older trusty image, because the newer images cause build errors with | ||
# psycopg2 that comes with CKAN<2.8: | ||
# "Error: could not determine PostgreSQL version from '10.1'" | ||
# see https://github.com/travis-ci/travis-ci/issues/8897 | ||
dist: trusty | ||
group: deprecated-2017Q4 | ||
|
||
# matrix | ||
python: | ||
- 2.7 | ||
env: | ||
- CKANVERSION=master | ||
- CKANVERSION=2.7 | ||
- CKANVERSION=2.8 | ||
|
||
# tests | ||
- "3.8" | ||
env: CKANVERSION=2.9 | ||
services: | ||
- postgresql | ||
- redis-server | ||
- postgresql | ||
- redis | ||
- docker | ||
install: | ||
- bash bin/travis-build.bash | ||
- pip install coveralls | ||
- bash bin/travis-build.bash | ||
- pip install coveralls | ||
- pip freeze | ||
script: sh bin/travis-run.sh | ||
after_success: | ||
- coveralls | ||
|
||
# additional jobs | ||
matrix: | ||
include: | ||
- name: "Flake8 on Python 3.7" | ||
dist: xenial # required for Python 3.7 | ||
cache: pip | ||
install: pip install flake8 | ||
script: | ||
- flake8 --version | ||
- flake8 . --count --max-complexity=10 --max-line-length=127 --statistics --exclude ckan,ckanext-saml2auth | ||
python: 3.7 | ||
# overwrite matrix | ||
env: | ||
- FLAKE8=true | ||
- CKANVERSION=master | ||
- coveralls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,5 @@ | ||
#!/bin/sh -e | ||
set -ex | ||
|
||
flake8 --version | ||
# stop the build if there are Python syntax errors or undefined names | ||
flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics --exclude ckan,ckanext-saml2auth | ||
pytest --ckan-ini=subdir/test.ini --cov=ckanext.saml2auth --disable-warnings ckanext/saml2auth/tests | ||
|
||
pytest --ckan-ini=subdir/test.ini \ | ||
--cov=ckanext.saml2auth | ||
|
||
# strict linting | ||
flake8 . --count --max-complexity=10 --max-line-length=127 --statistics --exclude ckan,ckanext-saml2auth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# encoding: utf-8 | ||
import logging | ||
import string | ||
import secrets | ||
from six import text_type | ||
|
||
from saml2.client import Saml2Client | ||
from saml2.config import Config as Saml2Config | ||
|
||
import ckan.model as model | ||
import ckan.authz as authz | ||
from ckan.common import config, asbool, aslist | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def saml_client(config): | ||
sp_config = Saml2Config() | ||
sp_config.load(config) | ||
client = Saml2Client(config=sp_config) | ||
return client | ||
|
||
|
||
def generate_password(): | ||
alphabet = string.ascii_letters + string.digits | ||
password = ''.join(secrets.choice(alphabet) for i in range(8)) | ||
return password | ||
|
||
|
||
def is_default_login_enabled(): | ||
return asbool( | ||
config.get('ckanext.saml2auth.enable_ckan_internal_login', | ||
False)) | ||
|
||
|
||
def update_user_sysadmin_status(username, email): | ||
sysadmins_list = aslist( | ||
config.get('ckanext.saml2auth.sysadmins_list')) | ||
user = model.User.by_name(text_type(username)) | ||
sysadmin = authz.is_sysadmin(username) | ||
|
||
if sysadmin and email not in sysadmins_list: | ||
user.sysadmin = False | ||
model.Session.add(user) | ||
model.Session.commit() | ||
elif not sysadmin and email in sysadmins_list: | ||
user.sysadmin = True | ||
model.Session.add(user) | ||
model.Session.commit() | ||
|
||
|
||
def activate_user_if_deleted(userobj): | ||
u'''Reactivates deleted user.''' | ||
if userobj.is_deleted(): | ||
userobj.activate() | ||
userobj.commit() | ||
log.info(u'User {} reactivated'.format(userobj.name)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,49 @@ | ||
# encoding: utf-8 | ||
import ckan.plugins as plugins | ||
import ckan.plugins.toolkit as toolkit | ||
|
||
from ckanext.saml2auth.views.saml2auth import saml2auth | ||
from ckanext.saml2auth import helpers as h | ||
|
||
|
||
class Saml2AuthPlugin(plugins.SingletonPlugin): | ||
plugins.implements(plugins.IConfigurer) | ||
plugins.implements(plugins.IBlueprint) | ||
plugins.implements(plugins.IConfigurable) | ||
plugins.implements(plugins.ITemplateHelpers) | ||
|
||
# ITemplateHelpers | ||
|
||
def get_helpers(self): | ||
return { | ||
'is_default_login_enabled': | ||
h.is_default_login_enabled | ||
} | ||
|
||
# IConfigurable | ||
|
||
def configure(self, config): | ||
# Certain config options must exists for the plugin to work. Raise an | ||
# exception if they're missing. | ||
missing_config = "{0} is not configured. Please amend your .ini file." | ||
config_options = ( | ||
'ckanext.saml2auth.idp_metadata.local_path', | ||
'ckanext.saml2auth.user_firstname', | ||
'ckanext.saml2auth.user_lastname', | ||
'ckanext.saml2auth.user_email' | ||
) | ||
for option in config_options: | ||
if not config.get(option, None): | ||
raise RuntimeError(missing_config.format(option)) | ||
|
||
# IBlueprint | ||
|
||
def get_blueprint(self): | ||
return [saml2auth] | ||
|
||
# IConfigurer | ||
|
||
def update_config(self, config_): | ||
toolkit.add_template_directory(config_, 'templates') | ||
toolkit.add_public_directory(config_, 'public') | ||
toolkit.add_resource('fanstatic', | ||
'saml2auth') | ||
toolkit.add_resource('fanstatic', 'saml2auth') |
Oops, something went wrong.