Skip to content

Commit 103c9f2

Browse files
committed
Chat demo and some minor fixes
1 parent 743c96f commit 103c9f2

File tree

5 files changed

+71
-67
lines changed

5 files changed

+71
-67
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ toolsVersion=2.0.0-final
3535
junitVersion=4.10
3636

3737
# The version of Yoke
38-
yokeVersion=1.0.1
38+
yokeVersion=1.0.2-SNAPSHOT

gradlew

100644100755
File mode changed.

src/main/resources/GroovyExample.groovy

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ import com.jetdrone.vertx.yoke.middleware.*
22
import com.jetdrone.vertx.yoke.GYoke
33
import com.jetdrone.vertx.yoke.engine.GroovyTemplateEngine
44
import com.jetdrone.vertx.yoke.util.Utils
5+
import org.vertx.java.core.json.JsonObject
56

67
import javax.crypto.Mac
78
import static groovy.json.JsonOutput.toJson
89
import static java.util.UUID.randomUUID
910

1011
def secret = Utils.newHmacSHA256("secret here")
11-
def storage = new HashMap<String, String>()
12+
def storage = vertx.sharedData.getMap('session.store')
1213

1314
def router = new GRouter()
1415
.get("/") {request ->
1516
request.response.sendFile "index.html"
1617
}
17-
.post("/apps/protect") {req ->
18-
req.response.end toJson("Hello world!")
18+
.post("/apps/protect") {request ->
19+
def body = request.formAttributes
20+
request.response.end toJson(body['content'])
1921
}
2022
.post("/auth/login") {request ->
2123
def body = request.formAttributes
@@ -28,6 +30,11 @@ def router = new GRouter()
2830
}
2931
request.response.end toJson(false)
3032
}
33+
.get("/auth/user") {request, next ->
34+
def uname = storage.get(request.sessionId == null ? "" : request.sessionId)
35+
def json = new JsonObject().putString("username", uname).putString("sessionId", request.sessionId)
36+
request.response.end json
37+
}
3138
.post("/auth/logout") {request ->
3239
def sid = request.getSessionId()
3340
storage.remove(sid == null ? "" : sid)
@@ -45,14 +52,27 @@ def secHandler = { request, next ->
4552
next.handle(null)
4653
}
4754

55+
def server = vertx.createHttpServer()
56+
def eb = vertx.eventBus
57+
4858
new GYoke(vertx)
4959
.engine('html', new GroovyTemplateEngine())
5060
.use(new ErrorHandler(true))
5161
.use(new CookieParser(secret))
5262
.use(new Session(secret))
5363
.use(new Logger(container.logger))
64+
.use(new BridgeSecureHandler("auth_address", "session.store" ))
5465
.use(new BodyParser())
5566
.use("/static", new Static("."))
5667
.use("/apps", secHandler)
5768
.use(router)
58-
.listen(8080)
69+
.listen(server)
70+
71+
def inboundPermitted = [
72+
[
73+
address : 'some-address',
74+
requires_auth : true
75+
]
76+
]
77+
vertx.createSockJSServer(server).bridge(prefix: '/eventbus', inboundPermitted, [[:]], 5 * 60 * 1000, 'auth_address')
78+
server.listen(8080)

src/main/resources/index.html

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html xmlns="http://www.w3.org/1999/xhtml">
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5-
<title>Angular authorization demo application</title>
5+
<title>Angular authorization and chat demo built with Yoke</title>
66

77
<link rel="stylesheet" href="/static/app/main.css" type="text/css" media="screen" />
88

@@ -42,50 +42,11 @@
4242

4343
<div id="content" ng-controller="ContentController">
4444

45-
<h1>HTTP Auth Interceptor Module Demo</h1>
46-
<h2>for AngularJS</h2>
45+
<h1>Angular authorization and chat demo built with Yoke</h1>
4746

48-
<h3>What is the purpose of this demo?</h3>
49-
<p>
50-
This demo application is to show you the implementation of the concept
51-
described in
52-
<a href="http://www.espeo.pl/2012/02/26/authentication-in-angularjs-application">
53-
Authentication in AngularJS (or similar) based application.
54-
</a>
55-
</p>
56-
<p>
57-
This application does not talk to real server, it uses
58-
<a href="http://docs.angularjs.org/api/ngMockE2E"> ngMockE2E module</a> instead.
59-
</p>
60-
<p>
61-
The HTTP Auth Interceptor module source code is available here:
62-
<br>
63-
<a href="https://github.com/witoldsz/angular-http-auth">
64-
https://github.com/witoldsz/angular-http-auth
65-
</a>
66-
</p>
67-
<p>
68-
The source code of this demo is available in the same project as
69-
a <a href="http://pages.github.com/">GitHub Pages</a> branch here:
70-
<br>
71-
<a href="https://github.com/witoldsz/angular-http-auth/tree/gh-pages">
72-
https://github.com/witoldsz/angular-http-auth/tree/gh-pages
73-
</a>
74-
</p>
75-
<hr>
7647
<br>&nbsp;
77-
<h3>Enter some data and submit to send and access public data.</h3>
78-
<form ng-submit="publicAction()">
79-
Enter public data:
80-
<input type="text" ng-model="publicData">
81-
<input type="submit" value="Submit"></input>
82-
</form>
83-
<p ng-repeat="p in publicContent">Server says: {{p}}</p>
84-
85-
<br/>
86-
<br/>
87-
88-
<h3>Enter some data and submit to send and access protected data.</h3>
48+
49+
<h3>Post some protected data. Note: "user/pass" for username and password.</h3>
8950
<form ng-submit="restrictedAction()">
9051
Enter restricted data:
9152
<input type="text" ng-model="restrictedData">
@@ -94,30 +55,25 @@ <h3>Enter some data and submit to send and access protected data.</h3>
9455
<p ng-repeat="r in restrictedContent">Server says: {{r}}</p>
9556

9657
<br/>
97-
<br/>
98-
99-
<h3>Press the button to log out.</h3>
100-
<input type="button" value="Log out" ng-click="logout()"></input>
101-
<p>
102-
The button will tell the server to forget about your session and also
103-
it will remove sensitive data from page.
104-
</p>
105-
<p>
106-
In real application it could do
107-
something else like:
108-
</p>
109-
<ul>
110-
<li>forcing login window to appear to hide data,</li>
111-
<li>reloading web page to purge application from received content or</li>
112-
<li>whatever you find appropriate...</li>
113-
</ul>
11458

59+
<br>&nbsp;
60+
<h3>Try to chat after login.</h3>
61+
<ul>
62+
<li ng-repeat="message in messages">{{message}}</li>
63+
</ul>
64+
<form ng-submit="sendMessage()">
65+
<input type="text" ng-model="messageText" placeholder="Type your message here" />
66+
<input type="submit" value="Send" />
67+
</form>
11568
</div>
11669

11770
<script src="/static/lib/jquery-1.7.2.js" type="text/javascript"></script>
11871
<script src="/static/lib/angular-1.0.1.js" type="text/javascript"></script>
11972
<script src="/static/lib/angular-mocks-1.0.1.js" type="text/javascript"></script>
12073
<script src="/static/lib/http-auth-interceptor.js" type="text/javascript"></script>
74+
<script src="/static/lib/sockjs-0.3.4.min.js" type="text/javascript"></script>
75+
<script src="/static/lib/vertxbus.js" type="text/javascript"></script>
76+
12177
<script src="/static/app/main.js" type="text/javascript"></script>
12278
<script src="/static/app/login.js" type="text/javascript"></script>
12379
<script src="/static/app/content.js" type="text/javascript"></script>

src/main/resources/static/app/content.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ angular.module('angular-auth-demo').controller({
55
$scope.restrictedContent = [];
66

77
$scope.publicAction = function() {
8-
$http.post('data/public', $scope.publicData).success(function(response) {
8+
var data = $.param({content: $scope.publicData});
9+
$http.post('apps/protect', data, {
10+
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
11+
}).success(function(response) {
12+
// this piece of code will not be executed until user is authenticated
913
$scope.publicContent.push(response);
1014
});
15+
1116
}
1217

1318
$scope.restrictedAction = function() {
14-
$http.post('apps/protect', $scope.restrictedData).success(function(response) {
19+
var data = $.param({content: $scope.restrictedData});
20+
$http.post('apps/protect', data, {
21+
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
22+
}).success(function(response) {
1523
// this piece of code will not be executed until user is authenticated
1624
$scope.restrictedContent.push(response);
1725
});
@@ -22,6 +30,26 @@ angular.module('angular-auth-demo').controller({
2230
$scope.restrictedContent = [];
2331
});
2432
}
33+
34+
var eb = new vertx.EventBus('http://localhost:8080/eventbus');
35+
$scope.messages = [];
36+
37+
eb.onopen = function() {
38+
eb.registerHandler('some-address', function(message) {
39+
console.log('received a message: ' + JSON.stringify(message));
40+
$scope.messages.push(message.content);
41+
$scope.$apply();
42+
});
43+
}
44+
45+
$scope.sendMessage = function() {
46+
$http.get('auth/user').success(function(response) {
47+
eb.sessionID = response.sessionId;
48+
eb.publish('some-address', {content: $scope.messageText});
49+
$scope.messageText = "";
50+
});
51+
};
52+
2553
}
2654

2755
});

0 commit comments

Comments
 (0)