Skip to content

Commit

Permalink
add support for tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
knudmoeller committed Jul 24, 2024
1 parent f23b8f3 commit e55a098
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Development

- Allow authorization via tokens, in addition to browser cookies and api keys.

## [0.2.7](https://github.com/berlinonline/ckanext-berlinauth/releases/tag/0.2.7)

_(2024-07-22)_
Expand Down
2 changes: 1 addition & 1 deletion ckanext/berlinauth/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.7
0.2.7-dev
6 changes: 6 additions & 0 deletions ckanext/berlinauth/auth_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import re

import ckan.lib.api_token as api_token
import ckan.lib.base as base
import ckan.model as model
from ckan.plugins.toolkit import config, asbool
Expand Down Expand Up @@ -86,6 +87,9 @@ def __call__(self, environ, start_response):
start_response(status, headers)
return [b'']

# TODO: instead of using this hacky function, we should maybe use ckan.views.identify_user()
# Problem: it will crash saying there is no application context (Flask)
# How do we get one?
def _get_user_for_apikey(self, environ):
# Adapted from https://github.com/ckan/ckan/blob/625b51cdb0f1697add59c7e3faf723a48c8e04fd/ckan/lib/base.py#L396
apikey_header_name = config.get(base.APIKEY_HEADER_NAME_KEY,
Expand All @@ -105,4 +109,6 @@ def _get_user_for_apikey(self, environ):
# check if API key is valid by comparing against keys of registered users
query = model.Session.query(model.User)
user = query.filter_by(apikey=apikey).first()
if not user:
user = api_token.get_user_from_token(apikey)
return user
7 changes: 6 additions & 1 deletion ckanext/berlinauth/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,9 @@ def get_actions(self):
# -------------------------------------------------------------------

def make_middleware(self, app, config):
return AuthMiddleware(app, config)
return AuthMiddleware(app, config)

# from ckan.config.middleware.flask_app import CKANFlask
# if isinstance(app, CKANFlask):
# app.wsgi_app = FeedMiddleware(app.wsgi_app)
# return app

0 comments on commit e55a098

Please sign in to comment.