This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

First commit. Steam OID authentication done.

  • Loading branch information...
RJacksonm1 committed Sep 5, 2013
0 parents commit 66b26fc28122b7faa98b4611fd78c4b2e9c80938
Showing with 169 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +25 −0 dotabank.py
  3. +14 −0 models.py
  4. BIN static/images/sits_small.png
  5. +5 −0 templates/dotabank.html
  6. +70 −0 templates/layout.html
  7. +52 −0 views.py
@@ -0,0 +1,3 @@
+*.pyc
+.idea/*
+mysql-connector*
@@ -0,0 +1,25 @@
+from flask import Flask
+from flask.ext.cache import Cache
+from flask.ext.openid import OpenID
+from flask.ext.sqlalchemy import SQLAlchemy
+import steam
+
+app = Flask(__name__)
+
+app.config.from_pyfile("settings.py")
+
+cache = Cache(app)
+oid = OpenID(app)
+
+db = SQLAlchemy(app)
+
+# Setup steamodd
+steam.api.key.set(app.config['STEAM_API_KEY'])
+steam.api.socket_timeout.set(10)
+
+
+# noinspection PyUnresolvedReferences
+from views import *
+
+if __name__ == '__main__':
+ app.run()
@@ -0,0 +1,14 @@
+from dotabank import db
+
+
+class User(db.Model):
+ __tablename__ = "users"
+ id = db.Column(db.Integer, primary_key=True)
+ name = db.Column(db.String(50), unique=True)
+
+ def __init__(self, id, name=None):
+ self.id = id
+ self.name = name
+
+ def __repr__(self):
+ return "<User {}>".format(self.name)
Binary file not shown.
@@ -0,0 +1,5 @@
+{% extends "layout.html" %}
+
+{% block content %}
+ Stuff
+{% endblock %}
@@ -0,0 +1,70 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>{{ title or "Dotabank" }}</title>
+ <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
+
+ <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc2/css/bootstrap.min.css" rel="stylesheet"/>
+ {%- block style %}
+ {%- endblock style %}
+</head>
+
+<body>
+ <div class="navbar navbar-static-top">
+ <div class="container">
+ <a class="navbar-brand" href="{{ url_for("index") }}">DOTABANK</a>
+ <ul class="nav navbar-nav">
+ <li><a href="#">1</a></li>
+ <li><a href="#">2</a></li>
+ <li><a href="#">3</a></li>
+ </ul>
+ <ul class="nav navbar-nav pull-right">
+ {% if g.user %}
+ <li class="dropdown">
+ <a class="dropdown-toggle" data-toggle="dropdown"> {{ g.user.name }} <span class="caret"></span></a>
+ <ul class="dropdown-menu">
+ <li><a href="{{ url_for("user", _id=g.user.id) }}">Profile</a></li>
+ <li><a href="{{ url_for("logout") }}">Logout</a></li>
+ </ul>
+ </li>
+ {% else %}
+ <li><a href="{{ url_for("login") }}"><img src="{{ url_for('static', filename='images/sits_small.png') }}" alt="Sign in with Steam"/></a></li>
+ {% endif %}
+ </ul>
+
+ </div>
+ </div>
+ <div class="container">
+ {% for category, message in get_flashed_messages(with_categories=true) %}
+ <div class="flash alert {{ "alert-" + category if category != "message" else ""}}">
+ <button type="button" class="close" data-dismiss="alert">&times;</button>
+ {{ message }}
+ </div>
+ {% endfor %}
+ {% block content %}
+ {% endblock %}
+ </div>
+ <hr>
+ <div class="container">
+ <p class="text-muted">© <a href="http://rjackson.me">RJ</a>, PROBABLY. <a href="http://steampowered.com">Powered by Steam</a>.</p>
+ </div>
+ {% block scripts %}
+ <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
+ <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc2/js/bootstrap.min.js"></script>
+ <script type="text/javascript">
+ // Javascript to enable link to tab
+ var hash = document.location.hash;
+ var prefix = "tab_";
+ if (hash) {
+ $('.nav-tabs a[href='+hash.replace(prefix,"")+']').tab('show');
+ }
+
+ // Change hash for page-reload
+ $('.nav-tabs a').on('click', function (e) {
+ window.location.hash = e.target.hash.replace("#", "#" + prefix);
+ });
+ </script>
+ {% endblock %}
+</body>
+</html>
@@ -0,0 +1,52 @@
+from flask import render_template, flash, redirect, request, session, g, url_for
+
+from dotabank import app, oid, steam, db
+from models import User
+
+# User authentication
+@app.route('/login/')
+@oid.loginhandler
+def login():
+ if g.user is not None:
+ return redirect(oid.get_next_url())
+ return oid.try_login('http://steamcommunity.com/openid')
+
+
+@oid.after_login
+def create_or_login(resp):
+ steam_id = long(resp.identity_url.replace("http://steamcommunity.com/openid/id/", ""))
+ account_id = int(steam_id & 0xFFFFFFFF)
+ g.user = User.query.filter(User.id == int(account_id & 0xFFFFFFFF)).first()
+
+ if not g.user:
+ g.user = User(int(account_id & 0xFFFFFFFF), steam.user.profile(steam_id).persona or account_id)
+
+ db.session.add(g.user)
+ db.session.commit()
+ session['user_id'] = g.user.id
+ flash('You are logged in as %s' % g.user.name, "success")
+ return redirect(oid.get_next_url())
+
+
+@app.before_request
+def before_request():
+ g.user = None
+ if 'user_id' in session:
+ g.user = User.query.filter(User.id == session['user_id']).first()
+
+
+@app.route('/logout/')
+def logout():
+ session.pop('user_id', None)
+ return redirect(oid.get_next_url())
+
+
+# Routes
+@app.route('/')
+def index():
+ return render_template("dotabank.html")
+
+
+@app.route("/user/<int:_id>")
+def user(_id):
+ return "abc"

0 comments on commit 66b26fc

Please sign in to comment.