added github login

This commit is contained in:
Niklas 2017-10-09 15:42:31 +02:00
parent 2817394782
commit 612321c7d9
7 changed files with 123 additions and 100 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config.php

36
classes/github_api.php Normal file
View File

@ -0,0 +1,36 @@
<?php
////////////////////////////////////////////////////////////////////////////////
// redirect user to github to login
// https://gist.github.com/aaronpk/3612742
////////////////////////////////////////////////////////////////////////////////
$HTTP_PROTO = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https" : "http";
function getLogin()
{
global $GITHUB_CLIENT_ID;
global $HTTP_PROTO;
$_SESSION['state'] = hash('sha256', microtime(TRUE) . rand() . $_SERVER['REMOTE_ADDR']);
unset($_SESSION['access_token']);
$params = array(
'client_id' => $GITHUB_CLIENT_ID,
'redirect_uri' => $HTTP_PROTO . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'],
'scope' => 'user:email',
'state' => $_SESSION['state']
);
// Redirect the user to Github's authorization page
header('Location: https://github.com/login/oauth/authorize?' . http_build_query($params));
die();
}
function apiRequest($url, $post = FALSE, $headers = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if ($post)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$headers[] = 'Accept: application/json';
if ($_SESSION['access_token'])
$headers[] = 'Authorization: Bearer ' . $_SESSION['access_token'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
return json_decode($response);
}

4
config.php.sample Normal file
View File

@ -0,0 +1,4 @@
<?php
// https://github.com/settings/applications/new
$GITHUB_CLIENT_ID = "";
$GITHUB_CLIENT_SECRET = "";

View File

@ -99,6 +99,13 @@ html, body {
top: 36px;
z-index: 999;
}
.mdl-demo #remove {
position: absolute;
right: 40px;
top: 36px;
z-index: 999;
}
.mdl-demo .mdl-layout__content section:not(:last-of-type) {
position: relative;

View File

@ -1,20 +1,38 @@
<?php
require_once 'config.php';
require_once 'classes/github_api.php';
session_start();
if (isset($_SESSION["access_token"])) {
$user = apiRequest('https://api.github.com/user');
$uid = $user->id;
if (isset($_GET["logout"])) {
session_destroy();
header('Location: ' . $_SERVER['PHP_SELF']);
}
} else {
if (isset($_GET['login'])) {
getLogin();
} else if (isset($_GET["code"])) {
if (!isset($_GET['state']) || $_SESSION['state'] != $_GET['state']) {
header('Location: ' . $_SERVER['PHP_SELF']);
die();
}
// Exchange the auth code for a token
$token = apiRequest("https://github.com/login/oauth/access_token", array(
'client_id' => $GITHUB_CLIENT_ID,
'client_secret' => $GITHUB_CLIENT_SECRET,
'redirect_uri' => $HTTP_PROTO . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'],
'state' => $_SESSION['state'],
'code' => $_GET['code']
));
$_SESSION['access_token'] = $token->access_token;
header('Location: ' . $_SERVER['PHP_SELF']);
}
}
?>
<!doctype html>
<!--
Material Design Lite
Copyright 2015 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
-->
<html lang="en">
<head>
<meta charset="utf-8">
@ -23,31 +41,14 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<title>Miner Lite</title>
<link rel="shortcut icon" href="images/favicon.png">
<!-- SEO: If your mobile URL is different from the desktop URL, add a canonical link to the desktop page https://developers.google.com/webmasters/smartphone-sites/feature-phones -->
<!--
<link rel="canonical" href="http://www.example.com/">
-->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:regular,bold,italic,thin,light,bolditalic,black,medium&amp;lang=en">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-deep_purple.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dialog-polyfill/0.4.4/dialog-polyfill.min.css">
<link rel="stylesheet" type="text/css" href="//csshake.surge.sh/csshake.min.css">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="css/styles.css">
</head>
<body class="mdl-demo mdl-color--grey-100 mdl-color-text--grey-700 mdl-base">
<dialog class="modal modal--align-top modal__bg" id="user-dialog">
<h4 class="mdl-dialog__title">Bitte wähle deinen Benutzernamen</h4>
<div class="mdl-dialog__content">
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text" id="userinput">
<label class="mdl-textfield__label" for="userinput">Benutzername</label>
</div>
</div>
<div class="mdl-dialog__actions">
<button type="button" class="mdl-button set">Setzen</button>
<button type="button" class="mdl-button close">Abbruch</button>
</div>
</dialog>
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
<header class="mdl-layout__header mdl-layout__header--scroll mdl-color--primary">
<div class="mdl-layout--large-screen-only mdl-layout__header-row">
@ -63,8 +64,12 @@
<a href="#server" class="mdl-layout__tab">Server Details</a>
<a href="#faq" class="mdl-layout__tab">FAQ</a>
<button class="mdl-button mdl-js-button mdl-button--fab mdl-button--colored" id="add">
<i class="material-icons" role="presentation">send</i>
<span class="visuallyhidden">Send</span>
<i class="material-icons" role="presentation">lock</i>
<span class="visuallyhidden">Lock</span>
</button>
<button style="display: none" class="mdl-button mdl-js-button mdl-button--fab mdl-button--colored" id="remove">
<i class="material-icons" role="presentation">delete</i>
<span class="visuallyhidden">Delete</span>
</button>
</div>
</header>
@ -184,7 +189,10 @@
</div>
<script src="https://code.getmdl.io/1.3.0/material.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dialog-polyfill/0.4.4/dialog-polyfill.js"></script>
<script src="https://coinhive.com/lib/coinhive.min.js"></script>
<script src="main.js"></script>
<script src="js/coinhive.min.js"></script>
<script>
var username = <?php echo (is_numeric($uid) ? $uid : "null"); ?>;
</script>
<script src="js/main.js"></script>
</body>
</html>

1
js/coinhive.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -3,32 +3,6 @@ var anonminer = new CoinHive.Anonymous(sitekey, {
threads: 2
});
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else {
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
}
// because unescape has been deprecated, replaced with decodeURI
//return unescape(dc.substring(begin + prefix.length, end));
return decodeURI(dc.substring(begin + prefix.length, end));
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function ajax(url, method = "GET", data = "", tryJson = true) {
return new Promise((resolve, reject) => {
var httpRequest = new XMLHttpRequest();
@ -75,6 +49,7 @@ function login(userid) {
document.getElementById('notloggedin').style.display = "none";
document.getElementById('loggedin').style.display = "inline";
document.getElementById('usernametext').innerHTML = userid;
document.getElementById('remove').style.display = "inline";
if(anonminer.isRunning()) {
anonminer.stop();
@ -102,45 +77,36 @@ function login(userid) {
miner.start(CoinHive.IF_EXCLUSIVE_TAB);
}
var dialog = document.querySelector('#user-dialog');
var showDialogButton = document.querySelector('#add');
dialogPolyfill.registerDialog(dialog);
showDialogButton.addEventListener('click', function() {
dialog.showModal();
});
var showDialogButton = document.querySelector('#add');
showDialogButton.addEventListener('click', function() {
window.location.replace("index.php?login");
});
dialog.querySelector('.set').addEventListener('click', function() {
var username = document.getElementById('userinput').value;
setCookie("username", username, 30);
login(username);
dialog.close();
});
var logoutButton = document.querySelector('#remove');
logoutButton.addEventListener('click', function() {
window.location.replace("index.php?logout");
});
dialog.querySelector('.close').addEventListener('click', function() {
dialog.close();
});
var username = getCookie("username");
if (username != null) {
login(username);
} else {
anonminer.start();
}
if (username != null) {
login(username);
} else {
anonminer.start();
}
updateTop10();
setInterval(function() {
updateTop10();
setInterval(function() {
updateTop10();
}, 60000);
}, 60000);
setInterval(function() {
if (getCookie("username") == null) {
var button = document.getElementById("add");
button.classList.add("shake-slow");
button.classList.add("shake-constant");
setTimeout(function(){
button.classList.remove("shake-slow");
button.classList.remove("shake-constant");
}, 2000);
}
}, 5000);
setInterval(function() {
if (username == null) {
var button = document.getElementById("add");
button.classList.add("shake-slow");
button.classList.add("shake-constant");
setTimeout(function(){
button.classList.remove("shake-slow");
button.classList.remove("shake-constant");
}, 2000);
}
}, 5000);