Created
August 6, 2011 14:38
-
-
Save gre/1129391 to your computer and use it in GitHub Desktop.
Revisions
-
gre revised this gist
Aug 12, 2011 . 1 changed file with 26 additions and 27 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,20 +3,16 @@ // with Play (scala) Framework // @see http://scala.playframework.org/ class Pusher { val appId = Play.configuration.getProperty("pusher.appId") val key = Play.configuration.getProperty("pusher.key") val secret = Play.configuration.getProperty("pusher.secret") import java.security.MessageDigest import java.math.BigInteger import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec def trigger(channel:String, event:String, message:String):HttpResponse = { val domain = "api.pusherapp.com" val url = "/apps/"+appId+"/channels/"+channel+"/events"; @@ -34,27 +30,30 @@ object Pusher { WS.url("http://"+domain+url+"?"+params+"&auth_signature="+WS.encode(signature)).body(body).post() } def byteArrayToString(data: Array[Byte]) = { val hash = new BigInteger(1, data).toString(16); "0"*(32-hash.length) + hash } def md5(s: String):String = byteArrayToString(MessageDigest.getInstance("MD5").digest(s.getBytes("US-ASCII"))); def sha256(s: String, secret: String):String = { val mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec( secret.getBytes(), "HmacSHA256")); val digest = mac.doFinal(s.getBytes()); String.format("%0" + (digest.length << 1) + "x", new BigInteger(1, digest)); } } /* Usage example : object WebSockets extends Pusher { val channel = Play.configuration.getProperty("websockets.channel") def trigger(event:String, message:String):HttpResponse = trigger(channel, event, message) } object Test { def test = WebSockets.trigger("hello", "{ \"message\": \"test\" }") } */ -
gre revised this gist
Aug 6, 2011 . 1 changed file with 18 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,6 +17,24 @@ object Pusher { import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec def trigger(channel:String, event:String, message:String):HttpResponse = { val domain = "api.pusherapp.com" val url = "/apps/"+appId+"/channels/"+channel+"/events"; val body = message val params = List( ("auth_key", key), ("auth_timestamp", (new Date().getTime()/1000) toInt ), ("auth_version", "1.0"), ("name", event), ("body_md5", md5(body)) ).sort( (a,b) => a._1 < b._1 ).map( o => o._1+"="+WS.encode(o._2.toString) ).mkString("&"); val signature = sha256( List("POST", url, params).mkString("\n"), secret ); WS.url("http://"+domain+url+"?"+params+"&auth_signature="+WS.encode(signature)).body(body).post() } def byteArrayToString(data: Array[Byte]) = { val bigInteger = new BigInteger(1,data); var hash = bigInteger.toString(16); @@ -39,22 +57,4 @@ object Pusher { val bigInteger = new BigInteger(1,digest); String.format("%0" + (digest.length << 1) + "x", bigInteger); } } -
gre revised this gist
Aug 6, 2011 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ // Implementing the publishing Pusher REST API // @see http://pusher.com/docs/rest_api // with Play (scala) Framework // @see http://scala.playframework.org/ object Pusher { val appId = "your_pusher_app_id" -
gre revised this gist
Aug 6, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,10 +7,10 @@ object Pusher { val secret = "your_pusher_secret" import play.libs.WS import play.libs.WS.HttpResponse import play.libs.ws.FunctionalWebResponse._ import java.security.MessageDigest import java.math.BigInteger import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec -
gre revised this gist
Aug 6, 2011 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,6 +6,9 @@ object Pusher { val key = "your_pusher_key" val secret = "your_pusher_secret" import play.libs.WS import play.libs.ws.FunctionalWebResponse._ import java.security.MessageDigest import play.libs.WS.HttpResponse import java.math.BigInteger -
gre created this gist
Aug 6, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ // Implementing the publishing Pusher REST API // @see http://pusher.com/docs/rest_api object Pusher { val appId = "your_pusher_app_id" val key = "your_pusher_key" val secret = "your_pusher_secret" import java.security.MessageDigest import play.libs.WS.HttpResponse import java.math.BigInteger import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec def byteArrayToString(data: Array[Byte]) = { val bigInteger = new BigInteger(1,data); var hash = bigInteger.toString(16); while(hash.length() < 32 ){ hash = "0" + hash; } hash; } def md5(s: String):String = { val messageDigest = MessageDigest.getInstance("MD5"); val digest = messageDigest.digest(s.getBytes("US-ASCII")); byteArrayToString(digest); } def sha256(s: String, secret: String):String = { val signingKey = new SecretKeySpec( secret.getBytes(), "HmacSHA256"); val mac = Mac.getInstance("HmacSHA256"); mac.init(signingKey); var digest = mac.doFinal(s.getBytes("UTF-8")); digest = mac.doFinal(s.getBytes()); val bigInteger = new BigInteger(1,digest); String.format("%0" + (digest.length << 1) + "x", bigInteger); } def trigger(channel:String, event:String, message:String):HttpResponse = { val domain = "api.pusherapp.com" val url = "/apps/"+appId+"/channels/"+channel+"/events"; val body = message val params = List( ("auth_key", key), ("auth_timestamp", (new Date().getTime()/1000) toInt ), ("auth_version", "1.0"), ("name", event), ("body_md5", md5(body)) ).sort( (a,b) => a._1 < b._1 ).map( o => o._1+"="+WS.encode(o._2.toString) ).mkString("&"); val signature = sha256( List("POST", url, params).mkString("\n"), secret ); WS.url("http://"+domain+url+"?"+params+"&auth_signature="+WS.encode(signature)).body(body).post() } }