简体   繁体   中英

Scalatra API Token authentication module

I am building a RESTful API on top of Scalatra. Being new to the framework, I'm not entirely sure if there's already a solution out there that'll help me authentication users against the API using some token based system (not OAuth). Doesn't have to be anything fancy, I just can't seem to find anything worthwhile on Google. Since the framework caters well to building REST APIs, perhaps there is something already out there to help auth users using a token based system?

Jos Dirksen has a tutorial which includes example code for HMAC auth with Scalatra, it's at:

http://www.smartjava.org/content/tutorial-getting-started-scala-and-scalatra-part-iii

It may or may not meet the needs of your implementation, but it'll give you an idea of one way to approach the problem.

If you need to generate a Unique, Hashed & Random token in scala for API Token calls, here is a sample class

https://gist.github.com/jeffsteinmetz/063bd3237033f3af2ed9

I implemented something based on Christoph Hartmann post on Bearer Authentication. https://lollyrock.com/posts/scalatra-bearer-authentication/ .

My code: https://gist.github.com/mawaldne/065ab8566e1de6db33cc947d166e4680

This is very quick and dirty and I think there are security issues. Timing attacks are possible. (See https://thoughtbot.com/blog/token-authentication-with-rails for more info). But for something quick it works. We're only using this for an internal API so Im fine with his approach. YMMV.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM