diff --git a/docs/administration.rst b/docs/administration.rst index 038858e7..3ba44c99 100644 --- a/docs/administration.rst +++ b/docs/administration.rst @@ -244,7 +244,7 @@ For more information about how to use social logins, see: `Satellizer = (3, 0): + jwt.decode(id_token, secret.decode('utf-8'), algorithms=[algo], audience=args['clientId']) + else: + jwt.decode(id_token, secret, algorithms=[algo], audience=args['clientId']) + except jwt.DecodeError: + return dict(message='Token is invalid'), 403 + except jwt.ExpiredSignatureError: + return dict(message='Token has expired'), 403 + except jwt.InvalidTokenError: + return dict(message='Token is invalid'), 403 + + headers = {'authorization': 'Bearer {0}'.format(access_token)} + + # retrieve information about the current user. + r = requests.get(user_api_url, headers=headers) + profile = r.json() + + user = user_service.get_by_email(profile['email']) + metrics.send('successful_login', 'counter', 1) + + # update their google 'roles' + roles = [] + + role = role_service.get_by_name(profile['email']) + if not role: + role = role_service.create(profile['email'], description='This is a user specific role') + roles.append(role) + + # if we get an sso user create them an account + if not user: + # every user is an operator (tied to a default role) + if current_app.config.get('LEMUR_DEFAULT_ROLE'): + v = role_service.get_by_name(current_app.config.get('LEMUR_DEFAULT_ROLE')) + if v: + roles.append(v) + + user = user_service.create( + profile['name'], + get_psuedo_random_string(), + profile['email'], + True, + profile.get('thumbnailPhotoUrl'), + roles + ) + + else: + # we add 'lemur' specific roles, so they do not get marked as removed + for ur in user.roles: + if ur.authority_id: + roles.append(ur) + + # update any changes to the user + user_service.update( + user.id, + profile['name'], + profile['email'], + True, + profile.get('thumbnailPhotoUrl'), # incase profile isn't google+ enabled + roles + ) + + # Tell Flask-Principal the identity changed + identity_changed.send(current_app._get_current_object(), identity=Identity(user.id)) + + return dict(token=create_token(user)) + + class Google(Resource): def __init__(self): self.reqparse = reqparse.RequestParser() @@ -317,10 +440,27 @@ class Providers(Resource): 'type': '2.0' }) + elif provider == "oauth2": + active_providers.append({ + 'name': current_app.config.get("OAUTH2_NAME"), + 'url': current_app.config.get('OAUTH2_REDIRECT_URI'), + 'redirectUri': current_app.config.get("OAUTH2_REDIRECT_URI"), + 'clientId': current_app.config.get("OAUTH2_CLIENT_ID"), + 'responseType': 'code', + 'scope': ['openid', 'email', 'profile', 'groups'], + 'scopeDelimiter': ' ', + 'authorizationEndpoint': current_app.config.get("OAUTH2_AUTH_ENDPOINT"), + 'requiredUrlParams': ['scope', 'state', 'nonce'], + 'state': 'STATE', + 'nonce': get_psuedo_random_string(), + 'type': '2.0' + }) + return active_providers api.add_resource(Login, '/auth/login', endpoint='login') api.add_resource(Ping, '/auth/ping', endpoint='ping') api.add_resource(Google, '/auth/google', endpoint='google') +api.add_resource(OAuth2, '/auth/oauth2', endpoint='oauth2') api.add_resource(Providers, '/auth/providers', endpoint='providers')