Skip to content

Commit

Permalink
Basic authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
ineentho committed May 15, 2015
1 parent c0f1acc commit af2544b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ main:
ports:
- "5000:5000"
environment:
- GOOGLE_CONSUMER_KEY
- GOOGLE_CONSUMER_SECRET
- FLASK_ENV
- DATABASE_CONNECTION_STRING
volumes:
- ./:/usr/src/app/
- ./cfg/:/usr/src/app/cfg
links:
- db
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Flask-SQLAlchemy
psycopg2

# Authentication (for Google+) and scrypt for password hashing (between server API)
authomatic
requests
scrypt

# Testing framework (nose), Flask-Testing for improved flask testing,
Expand Down
4 changes: 2 additions & 2 deletions runserver.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import server

env_flask_env = os.environ.get('FLASK_ENV', 'development') == 'development'
env_conn_string = os.environ.get('DATABASE_CONNECTION_STRING', 'postgresql://postgres@db:5432/')
env_flask_env = os.environ.get('FLASK_ENV') or 'development'
env_conn_string = os.environ.get('DATABASE_CONNECTION_STRING') or 'postgresql://postgres@db:5432/'


server.create_app({
Expand Down
53 changes: 23 additions & 30 deletions server/auth.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
import os
from flask import request, jsonify
import httplib2
from oauth2client import client
from server import app
from authomatic import Authomatic
from authomatic.adapters import WerkzeugAdapter
from authomatic.providers import oauth2
from flask import Flask, request, make_response, jsonify
import requests


CONFIG = {
'google': {
'class_': oauth2.Google,
'consumer_key': os.environ.get('GOOGLE_CONSUMER_KEY'),
'consumer_secret': os.environ.get('GOOGLE_CONSUMER_SECRET'),
'scope': oauth2.Google.user_info_scope,
},
}
def get_user(access_token):
resp = requests.get('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + access_token)
json = resp.json()
return json['user_id']

authomatic = Authomatic(CONFIG, 'is this needed?')

@app.route('/login/<provider>', methods=['GET', 'POST'])
def login(provider):
response = make_response()
result = authomatic.login(WerkzeugAdapter(request, response), provider)
def get_name(access_token):
resp = requests.get('https://www.googleapis.com/oauth2/v2/userinfo', headers={
'Authorization': 'Bearer ' + access_token
})
return resp.json()['name']

if result:
if result.user:
# Logged in
return jsonify({
'success': True,
'name': result.user.name
})

return jsonify({
'success': False,
result: result
})
return response
@app.route('/auth', methods=['POST'])
def login():
params = request.get_json()
access_token = params['access_token']
name = get_name(access_token)

return jsonify({
'success': True,
'name': name
})

0 comments on commit af2544b

Please sign in to comment.