Skip to main content

OAuth 2 server to server and Apache Oltu

Leaving apart some FUDs I think that RFC 6749 (aka The OAuth 2.0 Authorization Framework) has proven to be a really great  "tool" so far. One of the limitation of this spec though is that the 2 main flows Authorization Code Grant and Implicit Grant work reasonably well if there is some sort of human interaction and the user agent is available. What if one or both of these requirement are missing? One easy alternative would be to use the Resource Owner Password Credentials Grant flow. This would require the OAuth client to know the Resource Owner password. That is exactly why OAuth has been designed namely to avoid such situation. Another, more tempting, alternative would be to use a refresh token (that never expires). The best choice though IMHO is to use "tools" from another specification from the becoming-big OAuth specification family :)
The specification I am referring to is  the
JSON Web Token (JWT) Profile for OAuth 2.0 Client Authentication and Authorization Grants specification that "inhertis" from  Assertion Framework for OAuth 2.0 Client Authentication and Authorization Grants. This specifications makes and extensions usage of the concepts contained in the JWT and JWS protocols and it offers a great solution for solving an OAuth server to server scenario. In the real world there are already some big companies already providing support for this e.g. Google and SalesForce. If at this stage you feel a bit dizzy, fret not, you are not the only one :).
If you are still with me, I am going to show a really easy way to implement this protocol in Java using Apache Oltu. Apache Oltu lately (in version 1.0) introduced support for JWT and JWS. I will take as an example the Google implementation . Notes for the readers 1) I am not going to explain the JSON Web Token (JWT) Profile for OAuth 2.0 Client Authentication and Authorization Grants specification here, 2) I assume that you have already generated yout Google service-account credentials. In here I am  going to show an alternative way of Creating a JWT without using the Google API (sing Apache Oltu indeed). The Apache Oltu's way (differently than the Google API) will work with any OAuth Provider that supports OAuth server to server. Enough words for today, here is the code snippet:

 KeyStore keyStore = KeyStore.getInstance("PKCS12");  
       keyStore.load(new FileInputStream(P12_FILE), "notasecret".toCharArray());  
       java.security.PrivateKey privateKey = (java.security.PrivateKey) keyStore.getKey("privatekey", "notasecret".toCharArray());  
       PrivateKey pk = new PrivateKey(privateKey);  
       JWT jwt = new JWT.Builder()  
       .setClaimsSetIssuer("788732372078-pas6c4tqtudpoco2f4au18e00suedjtb@developer.gserviceaccount.com")  
       .setClaimsSetCustomField("scope", " https://www.googleapis.com/auth/plus.login")  
       .setClaimsSetAudience("https://accounts.google.com/o/oauth2/token")  
       .setClaimsSetIssuedAt(System.currentTimeMillis() / 1000)  
       .setClaimsSetExpirationTime(System.currentTimeMillis() / 1000 +3600)  
       .build();  
       String payload = new JWTClaimsSetWriter().write(jwt.getClaimsSet());  
       JWS jws = new JWS.Builder()  
       .setType("JWT")  
       .setAlgorithm(JwsConstants.RS256)      
       .setPayload(payload).sign(new SignatureMethodRSAImpl(JwsConstants.RS256), pk).build();  
       System.out.println("your assertion is "+new JWSWriter().write(jws));
now in order to get the access token using the above assertion just do:
 curl -d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=ASSERTION' https://accounts.google.com/o/oauth2/token  
have fun!

Comments

Anonymous said…
This comment has been removed by the author.
Anonymous said…
This comment has been removed by the author.
Anonymous said…
This comment has been removed by the author.
Anonymous said…
The snippet can't be used anymore. It relies on the trunk branch (aka development version) of Oltu library and in this version, the "sign" method has changed to become sign(SignatureMethod<SK, VK> method, SK signingKey).
Any edit to come ?

(And sorry for the bunch of deleted comments. It took me time to realize that "<" and ">" must be written as &lt; and &gt;).
ll said…
thanks Emmanuel for reporting. From the other end wouldn't make sense to keep the stable 1.0 version example?
Anonymous said…
The matter is that your snippet makes use of the SignatureMethodRSAImpl class which is not part of the 1.0 stable version. This class is available in the development version only.
ll said…
thanks a lot Emmanuel. Fair enough. I will update the post so :)

Popular posts from this blog

OpenSSL Key Recovery Attack on DH small subgroups (CVE-2016-0701)

Usual Mandatory Disclaimer: IANAC (I am not a cryptographer) so I might likely end up writing a bunch of mistakes in this blog post... tl;dr The OpenSSL 1.0.2 releases suffer from a Key Recovery Attack on DH small subgroups . This issue got assigned CVE-2016-0701 with a severity of High and OpenSSL 1.0.2 users should upgrade to 1.0.2f. If an application is using DH configured with parameters based on primes that are not "safe" or not Lim-Lee (as the one in RFC 5114 ) and either Static DH ciphersuites are used or DHE ciphersuites with the default OpenSSL configuration (in particular SSL_OP_SINGLE_DH_USE is not set) then is vulnerable to this attack.  It is believed that many popular applications (e.g. Apache mod_ssl) do set the  SSL_OP_SINGLE_DH_USE option and would therefore not be at risk (for DHE ciphersuites), they still might be for Static DH ciphersuites. Introduction So if you are still here it means you wanna know more. And here is the thing. In my last bl

Critical vulnerability in JSON Web Encryption (JWE) - RFC 7516

tl;dr if you are using go-jose , node-jose , jose2go , Nimbus JOSE+JWT or jose4j with ECDH-ES please update to the latest version. RFC 7516 aka JSON Web Encryption (JWE) hence many software libraries implementing this specification used to suffer from a classic Invalid Curve Attack . This would allow an attacker to completely recover the secret key of a party using JWE with Key Agreement with Elliptic Curve Diffie-Hellman Ephemeral Static (ECDH-ES) , where the sender could extract receiver’s private key. Premise In this blog post I assume you are already knowledgeable about elliptic curves and their use in cryptography. If not Nick Sullivan 's A (Relatively Easy To Understand) Primer on Elliptic Curve Cryptography or Andrea Corbellini's series Elliptic Curve Cryptography: finite fields and discrete logarithms are great starting points. Then if you further want to climb the elliptic learning curve including the related attacks you might also want to visit https://s

The Curious Case of WebCrypto Diffie-Hellman on Firefox - Small Subgroups Key Recovery Attack on DH

tl;dr Mozilla Firefox prior to version 72 suffers from Small Subgroups Key Recovery Attack on DH in the WebCrypto 's API. The Firefox's team fixed the issue r emoving completely support for DH over finite fields (that is not in the WebCrypto standard). If you find this interesting read further below. Premise In this blog post I assume you are already knowledgeable about Diffie-Hellman over finite fields and related attacks. If not I recommend to read any cryptography book that covers public key cryptography. Here is a really cool simple explanation by David Wong : I found a cooler way to explain Diffie-Hellman :D pic.twitter.com/DlPvGwZbto — David Wong (@cryptodavidw) January 4, 2020 If you want more details about Small Subgroups Key Recovery Attack on DH I covered some background in one of my previous post ( OpenSSL Key Recovery Attack on DH small subgroups (CVE-2016-0701) ). There is also an academic pape r where we examine the issue with some more rigors.