api authentication and parameter validation for scala

92 views
Skip to first unread message

Stevenson Cunanan Lee

unread,
May 30, 2016, 5:23:43 AM5/30/16
to play-framework
Hi, 

My question
I wish to ask what are the common options for api authentication and parameter validation for a scala api?
I'm currently using play 2.4 with reactive mongo.

Is there a way to bind it to the model? what are your suggestions?

Best Regards,
Stevenson Lee

Stevenson Cunanan Lee

unread,
Jun 6, 2016, 3:35:59 AM6/6/16
to play-framework
Alright... so after a long break I finally got somewhere. Someone please correct me if I am wrong but it seems to me that if I create an object
say a User

case class User(
uuid: Option[UUID],
name: String,
nickname: Option[String],
birthday: Double,
dateCreated: Option[Double],
dateUpdated: Option[Double]
) {}

to quote the play documentation: The Play JSON API provides implicit Writes for most basic types, such as Int,DoubleString, and Boolean
The model acts like a validation or is used as validation by the json library. 
This partially answers my question.

Now I'm trying to figure out how I can add certain data like date_created to the response. and then return an object together with a 201  or a 200.

Just to give  people context i'm studying https://github.com/arturopala/play-2.4-crud-with-reactive-mongo

def create = Action.async(parse.json) { implicit request =>
validateAndThen[E] {
entity =>
service.create(entity).map {
case Right(id) => Created(Json.toJson(entity)).withHeaders(LOCATION -> redirectUrl(id).url)
        case Left(err) => BadRequest(err)
}
}
}

so I can return the entity created with the response but I want to generate the creation date and update date...
any idea on how best to do this?

Best Regards,
Stevenson Lee

Stevenson Cunanan Lee

unread,
Jun 6, 2016, 3:57:56 AM6/6/16
to play-framework
I tried setting default value to the model but it does not work.
I found a way to validate.
I tried using require... however it seems require throws and error and creates a 500 response 
with setting default values I tried setting the default dateCreated and dateUpdated... but it does not work.
case class User(
uuid: Option[UUID],
    name: Option[String],
nickname: String,
company: String,
    birthday: Double,
dateCreated: Option[Double],
dateUpdated: Option[Double]
) {
  //require(birthday < 9000, s"Birthday has specific value but was $birthday.")

if (this.dateCreated == null) {
this.dateCreated -> new java.util.Date().getTime();
}
this.dateUpdated -> new java.util.Date().getTime();

}

On Monday, May 30, 2016 at 5:23:43 PM UTC+8, Stevenson Cunanan Lee wrote:

ido

unread,
Jun 6, 2016, 7:21:07 AM6/6/16
to play-framework
Maybe it does not answer your question,
but try using joda.DateTime for time not double. I think in 2.5 it works out of the box.

And get the current time from some injected object, so you can unit test it.

best,
ido

Stevenson Cunanan Lee

unread,
Jun 7, 2016, 3:31:08 AM6/7/16
to play-framework
Hi, 

Thanks for the response. I understand what you are suggesting but the problem I'm having is how to manipulate data that I receive from post validate, transform and manipulate it. 


Like this
class UsersController(service: UsersService)
extends CRUDController[User, UUID](service)(routes.UsersController.read) {
override def create = {
//modify the request json here to add default values
super.create();
}

I am thinking I can just modify the request but i find that there are so many ways to do things I dont know how to start.
I basically just want to get the json from the request body, validate it based on rules that I can compose modularly and reuse, and finally modify content before saving it to a mongo db.

Best Regards,
Stevenson Lee

Will Sargent

unread,
Jun 7, 2016, 9:01:39 AM6/7/16
to play-fr...@googlegroups.com
To get the JSON from the request body, use request.asJson


and then the validation logic you can compose with partial functions and pattern matching.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/24d0dbed-165c-4cb8-986d-18bd06fa6d27%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Will Sargent

unread,
Jun 7, 2016, 9:32:17 AM6/7/16
to play-fr...@googlegroups.com
As far as API authentication goes, Deadbolt is common:


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages