Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ORCID to registration form #19

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions indralab_auth_tools/indralab_auth_tools/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
create_access_token, set_access_cookies, unset_jwt_cookies, JWTManager

from flask import Blueprint, jsonify, request, redirect
from sqlalchemy.exc import IntegrityError

from indralab_auth_tools.log import is_log_running, set_user_in_log, \
set_role_in_log
from indralab_auth_tools.src.models import User, Role, BadIdentity, \
IntegrityError, start_fresh, AuthLog, UserDatabaseError
start_fresh, AuthLog, UserDatabaseError


auth = Blueprint('auth', __name__, template_folder='templates')

Expand Down Expand Up @@ -97,17 +99,18 @@ def register(auth_details, user_identity):
pass

data = request.json
missing = [field for field in ['email', 'password']
missing = [field for field in ['email', 'password', 'orcid']
if field not in data]
if missing:
auth_details['missing'] = missing
return jsonify({"message": "No email or password provided"}), 400
return jsonify({"message": "No email, orcid, or password provided"}), 400

auth_details['new_email'] = data['email']

new_user = User.new_user(
email=data['email'],
password=data['password']
password=data['password'],
orcid=data['orcid'],
)

try:
Expand Down Expand Up @@ -168,7 +171,7 @@ def login(auth_details, user_identity):

access_token = create_access_token(identity=current_user.identity())
logger.info("Produced new access token.")
resp = jsonify({'login': True, 'user_email': current_user.email})
resp = jsonify({'login': True, 'user_email': current_user.email, 'orcid': current_user.orcid})
set_access_cookies(resp, access_token)
return resp

Expand Down
26 changes: 17 additions & 9 deletions indralab_auth_tools/indralab_auth_tools/src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
from base64 import b64encode
from datetime import datetime
from time import sleep
from typing import Optional

from sqlalchemy import create_engine
from sqlalchemy.orm import relationship, backref, sessionmaker
from sqlalchemy import Boolean, DateTime, Column, Integer, \
String, ForeignKey, LargeBinary
from sqlalchemy.dialects.postgresql import JSON, JSONB

from sqlalchemy.exc import IntegrityError


from indralab_auth_tools.src.database import Base, engine
from indralab_auth_tools.src.database import Base, engine

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -122,14 +119,26 @@ class User(Base, _AuthMixin):
roles = relationship('Role',
secondary='roles_users',
backref=backref('users', lazy='dynamic'))
orcid = Column(String(19), nullable=True)

_label = 'email'
_identity_cols = {'id', 'email'}

@classmethod
def new_user(cls, email, password, **kwargs):
return cls(email=email.lower(), password=hash_password(password),
**kwargs)
def new_user(
cls,
email: str,
password: str,
orcid: str,
**kwargs,
) -> "User":
"""Create a user with a lowerased email and hashed password."""
return cls(
email=email.lower(),
password=hash_password(password),
orcid=orcid,
**kwargs,
)

@classmethod
def get_by_email(cls, email, verify=None):
Expand Down Expand Up @@ -228,4 +237,3 @@ def verify_password(hashed_password, guessed_password, maxtime=0.5):
except scrypt.error:
sleep(1)
return False

Loading