using json instead of xml responses
authorAzul <azul@leap.se>
Tue, 3 Jul 2012 11:50:26 +0000 (13:50 +0200)
committerAzul <azul@leap.se>
Tue, 3 Jul 2012 11:50:26 +0000 (13:50 +0200)
example/http-srp.rb
example/models/user.rb
example/views/authenticate.erb [deleted file]
example/views/handshake.erb [deleted file]
example/views/ok.erb [deleted file]
example/views/salt.erb [deleted file]
example/views/xml.erb [deleted file]

index ef43844..2e50cc8 100644 (file)
@@ -1,5 +1,6 @@
 require 'sinatra'
 require 'pp'
+require 'json'
 
 require 'models/user'
 require 'models/log'
@@ -19,12 +20,14 @@ end
 post '/register/salt/' do
   Log.clear
   @user = User.new(params.delete('I'))
-  erb :salt, :layout => false, :content_type => :xml
+  content_type :json
+  { :salt => @user.salt.to_s(16) }.to_json
 end
 
 post '/register/user/' do
   User.current.verifier = params.delete('v').hex
-  erb :ok, :layout => false, :content_type => :xml
+  content_type :json
+  { :ok => true }.to_json
 end
 
 get '/login' do
@@ -35,9 +38,10 @@ end
 post '/handshake/' do
   @user = User.current
   Log.log(:handshake, params)
-  @auth = @user.initialize_auth(params)
-  Log.log(:init_auth, @auth)
-  erb :handshake, :layout => false, :content_type => :xml
+  @handshake = @user.initialize_auth(params)
+  Log.log(:init_auth, @handshake)
+  content_type :json
+  @handshake.to_json
 end
 
 post '/authenticate/' do
@@ -45,7 +49,8 @@ post '/authenticate/' do
   Log.log(:authenticate, params)
   @auth = @user.authenticate(params)
   Log.log(:confirm_authentication, @auth)
-  erb :authenticate, :layout => false, :content_type => :xml
+  content_type :json
+  @auth.to_json
 end
 
 get '/verify' do
index d374d36..91bbffc 100644 (file)
@@ -25,13 +25,13 @@ class User
   def initialize_auth(params)
     self.srp = SRP::Server.new(self.salt, self.verifier)
     bb, u = self.srp.initialize_auth(params.delete('A').hex)
-    return {:B => bb, :u => u}
+    return {:s => self.salt.to_s(16), :B => bb.to_s(16)}
   end
 
   def authenticate(params)
     if m2 = self.srp.authenticate(params.delete('M').hex)
       self.active = true
-      return {:M2 => m2}
+      return {:M => m2.to_s(16)}
     else
       self.active = false
       return {:error => "Access Denied"}
diff --git a/example/views/authenticate.erb b/example/views/authenticate.erb
deleted file mode 100644 (file)
index a6bf80d..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<% if @auth[:M2] %>
-  <M><%="%x" % [@auth[:M2]]%></M>
-<% end %>
-<% if @auth[:error] %>
-  <error><%=@auth[:error]%></error>
-<% end %>
-
diff --git a/example/views/handshake.erb b/example/views/handshake.erb
deleted file mode 100644 (file)
index 49eca91..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<%= %Q(<r s="%x" B="%x" />) % [@user.salt, @auth[:B]] %>
diff --git a/example/views/ok.erb b/example/views/ok.erb
deleted file mode 100644 (file)
index b0d4f93..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<ok/>
diff --git a/example/views/salt.erb b/example/views/salt.erb
deleted file mode 100644 (file)
index 5b34b93..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<salt><%="%x" % @user.salt %></salt>
diff --git a/example/views/xml.erb b/example/views/xml.erb
deleted file mode 100644 (file)
index 0e2dcc2..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<%= yield %>