Posts Tagged ‘authentication’
Scala introduction — writing an OAuth library
I started out intending to write some scala examples against the twitter API, however I soon discovered I needed OAuth first. Given that I use OAuth all the time at work I figured I could probably do with learning about it first-hand, while learning scala.
org.junit.rules._
I chose to test drive it with JUnit 4.7 and NetBeans.
NetBeans works almost immediately with scala, and has support for project templates etc — even scala JUnit fixtures.
UPDATE (2010-04-27) I have since discovered IntelliJ to be much better, and there is now a free community edition. IntelliJ supports scala without any fiddling around.
JUnit mostly works, though rules don’t and neither do some matchers. Even though rules don’t work, I have included it anyway because I have the t-shirt.
You can find the project on github.
Important abstractions
- SignatureBaseString.
- Characterized by three ampersand-separated segments: verb, uri, parameters.
- URL Encoding must conform to RFC 3986, and the following characters should are consider unreserved so should not be encoded:
ALPHA, DIGIT, ‘-‘, ‘.’, ‘_’, ‘~’
- Signature.
- Signature is a keyed-Hash Message Authentication Code (HMAC).
- Consumer secret required part of HMAC secret key.
- Token secret is optionally included in HMAC secret key:
(consumer_secret, token_secret) => uri_encoded_consumer_secret&[uri_encoded_token_secret]
- OAuthCredential. Represents the secret key(s) used to create the HMAC signature. OAuth requires a consumer credential, and optionally a token credential, representing the end user.
Now that these core concepts are complete, I am working on high-level policy, like classes for generating signed URLs and authorization headers.
Notes
JUnit — expecting exceptions in scala
Assuming JUnit 4.x, a test can expect an exception using the test annotation:
Java:
@Test(expected=IllegalArgumentException.class) public void ExampleThrowsException(){ throw new IllegalArgumentException(); }
This needs to be modified for scala:
Scala:
@Test { val expected=classOf[IllegalArgumentException] } def ExampleThrowsException { throw new IllegalArgumentException }
The reason for it is outlined here in the Java annotations section on named parameters.
Here is the documentation for scala annotations. Seealso: the documentation for scala 2.7.3 (includes dbc).
Closures and return
The return statement immediately returns from the current method, even if you’re within a closure. Omit return in this case — return is optional anyway.
When to use semicolon line terminator
Never — apart from:
- When a method returns Unit (equivalent to void) and you aren’t using return keyword. [TBD: Add example].
How to use blocks
var count = 1
times(2) { println("Printed " + count + " times")}
protected def times(count : Int)(block : => Unit) = {
1.to(count).foreach((_) => block)
}
Seealso: some executable examples on github
References
- Scala oauth lib (Github)
- OAuth specification
- Scala 2.7.3 documentation
- OAuth ruby gem
- OAuth test client
ALPHA, DIGIT, '-', '.', '_', '~'