Skip to content

Commit

Permalink
provide default redirect_uri route with oauth_result.tpl template fil…
Browse files Browse the repository at this point in the history
…e rendering

* add default example system_templates/oauth_result.tpl
* add controller with /oauth/authentication/result url when
oauth_result.tpl is rendered
* add documentation for GET /oauth/v2/auth_login route
  • Loading branch information
ahilles107 committed Apr 8, 2015
1 parent 752e038 commit 88837e1
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 27 deletions.
5 changes: 5 additions & 0 deletions newscoop/application/configs/symfony/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ fos_oauth_server_authorize:
oauth_login:
pattern: /oauth/v2/auth_login
defaults: { _controller: NewscoopGimmeBundle:Oauth:login }
methods: GET
oauth_login_check:
pattern: /oauth/v2/auth_login_check
oauth_authentication_result:
resource: "@NewscoopGimmeBundle/Controller/OauthController.php"
prefix: /
type: annotation
gimme:
type: rest
prefix: /api
Expand Down
26 changes: 0 additions & 26 deletions newscoop/src/Newscoop/GimmeBundle/Controller/DefaultController.php

This file was deleted.

34 changes: 33 additions & 1 deletion newscoop/src/Newscoop/GimmeBundle/Controller/OauthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,29 @@
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;

class OauthController extends Controller
{
/**
* Login user with oauth v2
*
* Find out more informations about Newscoop REST API authentiocation here: [click me][1]
*
* [1]: http://docs.sourcefabric.org/projects/newscoop-restful-api/en/master/tutorial.html
* @ApiDoc(
* statusCodes={
* 200="Returned when successful"
* },
* parameters={
* {"name"="client_id", "dataType"="integer", "required"=true, "description"="Your client id, for example 9_1irxa0qcy3ms48c8c8wsgcgsc04k0s0w0g0sg4cco4kocoowoo"},
* {"name"="redirect_uri", "dataType"="string", "required"=true, "description"="The uri of your client web application, for example http://myapp.example.com/. This must match the URI you added in the Newscoop Admin Interface above. Remember to encode the URI."},
* {"name"="response_type", "dataType"="string", "required"=true, "description"="Value must be: 'token'"},
* }
* )
*/
public function loginAction(Request $request)
{
$session = $request->getSession();
Expand All @@ -40,6 +60,18 @@ public function loginAction(Request $request)
$smarty->assign('error', $error);
$smarty->assign('targetPath', $request->getSession()->get('_security.oauth_authorize.target_path'));

return new Response($templatesService->fetchTemplate('oauth_login.tpl'));
return new Response($templatesService->fetchTemplate('oauth_login.tpl'), 200, array('Content-Type' => 'text/html'));
}

/**
* @Route("/oauth/authentication/result", defaults={"_format"="json"}, options={"expose"=true}, name="oauth_authentication_result")
* @Method("GET")
*/
public function defaultOauthRedirectAction(Request $request)
{
$templatesService = $this->get('newscoop.templates.service');
$smarty = $templatesService->getSmarty();

return new Response($templatesService->fetchTemplate('oauth_result.tpl'), 200, array('Content-Type' => 'text/html'));
}
}
89 changes: 89 additions & 0 deletions newscoop/themes/system_templates/js/jquery.cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*jslint browser: true */ /*global jQuery: true */

/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/

// TODO JsDoc

/**
* Create a cookie with the given key and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
* used when the cookie was set.
*
* @param String key The key of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
* If set to null or omitted, the cookie will be a session cookie and will not be retained
* when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
* require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/

/**
* Get the value of a cookie with the given key.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String key The key of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function (key, value, options) {

// key and value given, set cookie...
if (arguments.length > 1 && (value === null || typeof value !== "object")) {
options = jQuery.extend({}, options);

if (value === null) {
options.expires = -1;
}

if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}

return (document.cookie = [
encodeURIComponent(key), '=',
options.raw ? String(value) : encodeURIComponent(String(value)),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}

// key and possibly options given, get cookie...
options = value || {};
var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
67 changes: 67 additions & 0 deletions newscoop/themes/system_templates/oauth_result.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{{ dynamic }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Newscoop Oauth Authentication Result</title>

<!-- Bootstrap core CSS -->
<link href="/themes/system_templates/css/bootstrap.min.css" rel="stylesheet">
<link href="/themes/system_templates/css/main.css" rel="stylesheet">
<style type="text/css" media="screen">
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #eee;
}
#container {
max-width: 320px;
padding: 15px;
margin: 0 auto;
}
</style>
</head>

<body>
<div id="container">
<img src="/themes/system_templates/img/newscoop_logo_big.png" />
<h1 class="form-signin-heading text-muted">Authentication is finished</h1>
<p>Check result in this page url (with javascript) and continue with returned data</p>
</div>

<!-- Placed at the end of the document so the pages load faster -->
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="/themes/system_templates/js/jquery.cookie.js"></script>
<script src="/themes/system_templates/js/bootstrap.min.js"></script>
<script type="text/javascript">
function getHashParams() {
var hashParams = {};
var e,a = /\+/g,r = /([^&;=]+)=?([^&;]*)/g,d = function (s) { return decodeURIComponent(s.replace(a, " ")); },q = window.location.hash.substring(1);
while (e = r.exec(q))hashParams[d(e[1])] = d(e[2]);
return hashParams;
}
var hashParams = getHashParams();
// check if authentication was succesfull and play with access_token
if (jQuery.inArray("access_token", hashParams)) {
console.log('Your access_token is: ' + hashParams.access_token);
if ($.cookie('newscoop_access_token') == null || $.cookie('newscoop_access_token') != hashParams.access_token) {
// create new cookie with access_token value
console.log('Creating cookie with access_token value');
var date = new Date();
date.setTime(date.getTime() + (hashParams.expires_in * 1000));
$.cookie('newscoop_access_token', hashParams.access_token, { expires: date, path: '/' });
} else {
console.log('You have valid access_token under "newscoop_access_token" cookie');
}
} else if (jQuery.inArray("error", hashParams)) {
// there was an error on authentication process
console.log('error:' + hashParams.error);
}
</script>
</body>
</html>
{{ /dynamic }}

0 comments on commit 88837e1

Please sign in to comment.