Skip to content

Commit

Permalink
Add supoort for #4
Browse files Browse the repository at this point in the history
although not too well tested and completedly undocumented as of yet
  • Loading branch information
thefinn93 committed Nov 27, 2016
1 parent 6366b9d commit c1ee4ea
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
26 changes: 18 additions & 8 deletions piston/register/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
along with Piston. If not, see <http://www.gnu.org/licenses/>.
"""

from flask import Blueprint, render_template, request
from flask import Blueprint, render_template, request, url_for, current_app
from urllib.parse import urlparse

import piston
Expand All @@ -27,12 +27,16 @@
def checkconfig():
"""Retreive a configuration from a given URL and ensure that it complies with the rules."""
config = request.args.copy()
if "redirect_url" not in config:
raise exceptions.ConfigurationException("You're is missing a redirect_url!", config)
redirect = urlparse(config['redirect_url'])
if redirect.scheme != "https":
raise exceptions.InsecureRedirectException("The redirect_url must be https")
config['domain'] = redirect.netloc
if "desktop" in config:
if "name" not in config:
raise exceptions.ConfigurationException("You must specify a name for desktop tokens!")
elif "redirect_url" not in config:
raise exceptions.ConfigurationException("You're is missing a redirect_url!")
else:
redirect = urlparse(config['redirect_url'])
if redirect.scheme != "https":
raise exceptions.InsecureRedirectException("The redirect_url must be https")
config['domain'] = redirect.netloc
return config


Expand All @@ -43,6 +47,7 @@ def register_page():
config = checkconfig()
return render_template("register.html",
c=config,
desktop="desktop" in config,
root=request.headers['Host'])
except exceptions.ConfigurationException as e:
return render_template("error.html", error=e)
Expand All @@ -60,7 +65,12 @@ def post_register():
subscription=subscription)
piston.db.session.add(registration)
piston.db.session.commit()
token = "https://%s:%s@%s%s" % ("token", registration.token,
current_app.config.get("SERVER_NAME", "localhost"),
url_for('notification.create'))
return render_template("post_register.html", redirect_url=request.form.get("redirect_url"),
registration=registration, nonce=request.form.get("nonce", None))
registration=registration, token=token,
name=request.form.get('name'),
nonce=request.form.get("nonce"))
else:
return "okay, bye"
8 changes: 7 additions & 1 deletion piston/static/js/post_register.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ You should have received a copy of the GNU General Public License
along with Piston. If not, see <http://www.gnu.org/licenses/>.
*/

document.querySelector(".redirect-form").submit();
if(document.querySelector(".redirect-form") !== null) {
document.querySelector(".redirect-form").submit();
}

if(document.querySelector(".token-box") !== null) {
document.querySelector(".token-box").select();
}
17 changes: 10 additions & 7 deletions piston/templates/post_register.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@

{% extends 'base.html' %}
{% block title %}Register{% endblock %}
{% block content %}
<div class="card mx-auto">

{% block body %}
{% if redirect_url is none %}
<div class="card mx-auto single-card-page">
<div class="card-block">
<h4 class="card-title"></h4>
<h4 class="card-title">Authorization Created</h4>
<p class="card-subtitle text-muted">
Forwarding you back to <code>{{ redirect_url }}</code> with token <code>{{ registration.token }}</code>
Below is your token, paste it into {{ name }}
</p>
</div>

<div class="card-block">
<input type="text" class="form-control token-box" value="{{ token }}" />
</div>
</div>

{% else %}
<form class="redirect-form" action="{{ redirect_url }}" method="post">
{% if nonce is not none %}<input type="hidden" name="nonce" value="{{ nonce }}" />{% endif %}
<input type="hidden" name="token" value="{{ registration.token }}" />
</form>
{% endif %}
{% endblock %}

{% block js %}
Expand Down
7 changes: 4 additions & 3 deletions piston/templates/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
<div class="card-block">
<h4 class="card-title">Allow {% if c.name is string %}{{ c.name }}{% else %}{{ c.domain }}{% endif %} to send you notifications?</h4>
<p class="card-subtitle text-muted">
{% if c.name is string %}{{ c.name }} (<code>{{ c.domain }}</code>){% else %}{{ c.domain }}{% endif %} would like to send push notifications to your device.
You can edit this preference at any time by visiting <a href="{{ url_for('index', _external=true) }}">{{ root }}</a>
{% if desktop %}{{ c.name }}{% else %}{% if c.name is string %}{{ c.name }} (<code>{{ c.domain }}</code>){% else %}{{ c.domain }}{% endif %}{% endif %}
would like to send push notifications to your device. You can edit this preference at any time by visiting
<a href="{{ url_for('index', _external=true) }}">{{ root }}</a>
</p>
</div>

Expand All @@ -41,7 +42,7 @@ <h4 class="card-title">Allow {% if c.name is string %}{{ c.name }}{% else %}{{ c

<div class="card-block text-xs-center yes-notifications hidden">
<form method="post" action="{{ url_for('register.post_register') }}" class="approval-form">
<input type="hidden" name="redirect_url" value="{{ c.redirect_url }}" />
{% if not desktop %}<input type="hidden" name="redirect_url" value="{{ c.redirect_url }}" />{% endif %}
<input type="hidden" name="name" value="{{ c.name }}" />
<input type="hidden" name="subscription" value="unset" />
{{ csrf_token() | safe }}
Expand Down

0 comments on commit c1ee4ea

Please sign in to comment.