Skip to content

Instantly share code, notes, and snippets.

View guillaumebort's full-sized avatar

Guillaume Bort guillaumebort

View GitHub Profile
@guillaumebort
guillaumebort / 1.sql
Created May 25, 2012 15:17
Play 2.0/Anorm
# --- !Ups
CREATE TABLE users(
email VARCHAR(255) NOT NULL PRIMARY KEY,
name VARCHAR(255)
);
CREATE TABLE subjects(
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
title LONGTEXT NOT NULL,
@guillaumebort
guillaumebort / Secured.scala
Created April 7, 2012 12:05
HTTP Basic Authorization for Play 2.0
def Secured[A](username: String, password: String)(action: Action[A]) = Action(action.parser) { request =>
request.headers.get("Authorization").flatMap { authorization =>
authorization.split(" ").drop(1).headOption.filter { encoded =>
new String(org.apache.commons.codec.binary.Base64.decodeBase64(encoded.getBytes)).split(":").toList match {
case u :: p :: Nil if u == username && password == p => true
case _ => false
}
}.map(_ => action(request))
}.getOrElse {
Unauthorized.withHeaders("WWW-Authenticate" -> """Basic realm="Secured"""")
@guillaumebort
guillaumebort / gist:8314512
Last active July 25, 2016 02:19
Force HTTPS decorator for Play framework
def forceHttps(handler: Handler, request: RequestHeader): Handler = {
(isHTTPSRequired, isRequestSecure(request), request.method, request.uri) match {
// HTTPS is supported but not required for the API
case (_, _, _, uri) if uri.startsWith("/api") => handler
// HTTPS is required here, redirect GET requests
case (true, false, "GET", uri) => Action(Results.Redirect(s"https://${request.domain}${request.uri}"))
// HTTPS is required but we can't redirect
package controllers;
import play.*;
import play.mvc.*;
import views.html.*;
public class Application extends Controller {
public static Result index(String name) {
request.headers.get(CONTENT_TYPE) match {
case Some(ct) if ct.trim.startsWith("multipart/form-data") =>
filterLogger.trace("[CSRF] request is multipart/form-data")
checkMultipart(request, token, next)
case Some(ct) if ct.trim.startsWith("application/x-www-form-urlencoded") =>
filterLogger.trace("[CSRF] request is application/x-www-form-urlencoded")
checkFormUrlEncodedBody(request, token, next)
case Some(ct) if ct.trim.startsWith("text/plain") =>
filterLogger.trace("[CSRF] request is text/playn")
checkTextBody(request, token, next)
package controllers
import play.api._
import play.api.mvc._
import play.api.templates._
import reactivemongo.api._
import play.modules.reactivemongo._
import play.modules.reactivemongo.json.collection.JSONCollection
{"id":26,"label":"document","children":[{"id":14,"label":"heading1","children":[{"id":15,"content":"SURFACEPAD “THE ONLY CASE I WOULD CONSIDER USING WITH AN IPHONE”","parents":"14-26","tags":[]}],"parents":"26","tags":[]},{"id":16,"label":"paragraph","children":[{"id":17,"content":"This SurfacePad review is just so good we had to share it with you. If you’ve been following our SurfacePad for iPhone news, you already know it’s a product for, well, people who would rather their iPhone be au naturale…but they want to protect the screen and back from keys and hard surfaces. Gizmodo editor Jesus Dias gets it. In his doesn’t-mince-words-words:","parents":"16-26","tags":[]}],"parents":"26","tags":[]},{"id":18,"label":"paragraph","children":[{"id":19,"content":"“No, you don’t need a damn case for your iPhone 5—but if you have butterfingers and you must use a case, this seems to me like the only acceptable one. The SurfacePad is not only simple and elegant—complementing the iPhone’s design rather than turning it into
{"id":26,"label":"document","children":[{"id":14,"label":"heading1","children":[{"id":15,"content":"SURFACEPAD “THE ONLY CASE I WOULD CONSIDER USING WITH AN IPHONE”","parents":"14-26","tags":[]}],"parents":"26","tags":[]},{"id":16,"label":"paragraph","children":[{"id":17,"content":"This SurfacePad review is just so good we had to share it with you. If you’ve been following our SurfacePad for iPhone news, you already know it’s a product for, well, people who would rather their iPhone be au naturale…but they want to protect the screen and back from keys and hard surfaces. Gizmodo editor Jesus Dias gets it. In his doesn’t-mince-words-words:","parents":"16-26","tags":[]}],"parents":"26","tags":[]},{"id":18,"label":"paragraph","children":[{"id":19,"content":"“No, you don’t need a damn case for your iPhone 5—but if you have butterfingers and you must use a case, this seems to me like the only acceptable one. The SurfacePad is not only simple and elegant—complementing the iPhone’s design rather than turning it into
{
"Main" : {
"body" : {
"type" : "StructuredText",
"config" : {
"imageConstraint" : {
"width" : 580.0
},
"minHeight" : "400px"
}
@guillaumebort
guillaumebort / gist:4546727
Last active December 11, 2015 04:39
Hoho, I've something that will allow to write an awesome Scala Macro for #playframework
> compile
[info] Compiling 1 Scala source to /private/tmp/my/target/scala-2.10/classes...
> Analysing SQL: select name, age from people where age > ?
-- Parameters:
java.lang.Integer
-- Returns:
java.lang.String
java.lang.Integer
-------------