Skip to content

Commit ff8f945

Browse files
committed
Update auto
1 parent 89d41d8 commit ff8f945

File tree

7 files changed

+155
-0
lines changed

7 files changed

+155
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
# Oauth
2+
23
Login with GitHub.
4+
5+
# Config
6+
7+
1. clone to your server.
8+
2. change setting in 'gitauth/git.php'
9+
3. test
10+
11+
# use
12+
13+
it will go to get user info with github.

callback.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
require( 'gitauth/git.php' );
3+
4+
$git->git_response_callback();
5+
6+
?>

gitauth/Gitauth.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
class Gitauth {
4+
function __construct($config){
5+
$this->config = (object) $config;
6+
}
7+
function git_response_callback(){
8+
if(!isset($_SESSION['response'])){
9+
if($this->config->client_id != '' ||
10+
$this->config->client_secreet != '' ||
11+
$this->config->code != '' ||
12+
$this->config->redirect_to != ''){
13+
14+
//GET USER ACCESS_TOKEN
15+
$ch = curl_init();
16+
17+
curl_setopt($ch, CURLOPT_URL,"https://github.com/login/oauth/access_token");
18+
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
19+
curl_setopt($ch, CURLOPT_POST, 1);
20+
curl_setopt($ch, CURLOPT_POSTFIELDS,'client_id='.$this->config->client_id.
21+
'&client_secret='.$this->config->client_secreet.
22+
'&code='.$this->config->code.
23+
'&accept=application/json');
24+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
25+
26+
$server_output = curl_exec($ch);
27+
$_SESSION['access_token'] = json_decode($server_output);
28+
$user = $_SESSION['access_token'];
29+
30+
curl_close ($ch);
31+
32+
33+
34+
35+
//GET USER RESPONSE
36+
$ch = curl_init();
37+
38+
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/user?access_token='.$user->access_token);
39+
curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: '.$this->config->app_name,'Accept: application/json'));
40+
curl_setopt($ch, CURLOPT_HEADER, 0);
41+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
42+
43+
$get_response = curl_exec($ch);
44+
$_SESSION['response'] = $get_response;//json_decode($get_response);
45+
46+
curl_close($ch);
47+
48+
header('location: '.$this->config->redirect_to);
49+
50+
}else{
51+
echo 'Some config is missing';
52+
}
53+
}else{
54+
header('location: '.$this->config->redirect_to);
55+
}
56+
}
57+
58+
function git_authorize_url(){
59+
return 'https://github.com/login/oauth/authorize?scope='.$this->config->scope.'&client_id='.$this->config->client_id;
60+
}
61+
function git_user_token(){
62+
return $_SESSION['access_token']->access_token;
63+
}
64+
function git_user_data(){
65+
return $_SESSION['response'];
66+
}
67+
function git_user_logout($redirect){
68+
session_destroy();
69+
70+
header('location: '.$redirect);
71+
}
72+
}
73+
?>

gitauth/git.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
session_start();
3+
4+
require( 'Gitauth.php' );
5+
6+
//set default value
7+
$code = 0;
8+
9+
//get response code from github callback
10+
if(isset($_GET['code'])){
11+
$code = $_GET['code'];
12+
}
13+
14+
#=================================================================#
15+
# First you must register your application #
16+
# here https://github.com/settings/applications/new #
17+
# then you has detail of your application #
18+
# like client_id, client_secret next #
19+
# copy and put on the config below #
20+
# #
21+
# #
22+
# Rate, Fork and Share :) #
23+
#=================================================================#
24+
25+
$config = array('app_name' => '', //your application name
26+
'client_id' => '', //change to your application client id
27+
'client_secreet' => '', //change to your application client secreet
28+
'redirect_to' => '', //redirect to response page after callback like "response.php"
29+
'scope' => '', //set scope aplication
30+
'code' => $code //this automaticaly fill after authorize from github
31+
);
32+
33+
$git = new Gitauth( $config );
34+
35+
?>

index.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php require( 'gitauth/git.php' ); ?>
2+
<html>
3+
<head>
4+
</head>
5+
<body>
6+
<p>
7+
Please Wait...
8+
<?php $url=$git->git_authorize_url(); echo "<script>window.location.href='$url'</script>"; ?>
9+
</p>
10+
</body>
11+
</html>

logout.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
require( 'gitauth/git.php' );
3+
4+
$redirect = 'http://arisharyanto.app-show.net/gitauth/';
5+
$redirect = '/oauth/';
6+
$git->git_user_logout($redirect);
7+
?>

response.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
require( 'gitauth/git.php' );
3+
4+
if($git->git_user_token()){
5+
$user = $git->git_user_data();
6+
$arr = json_decode($user, true);
7+
$name=$arr['login'];
8+
echo "User info is in 'arr'";
9+
}else{
10+
echo 'Please Login to access this page';
11+
}
12+
?>

0 commit comments

Comments
 (0)