Spring Security Oauth with existing Oauth token - java

I'm currently building an application with Spring and I am trying to connect to an external application. The application's resources are protected by Oauth 1.0 and I already have the consumer key, consumer secret, oauth token, and oauth token secret. Is there a way to just use this information to obtain the resources or do I have to request a new oauth token every time? If I have to get a new token every time, then is there a good tutorial on how to do this? It would be much appreciated.

It depends on how the external application is built.
If the token you have is an access token and never expires - then you don't need to get a new token every time - just use it till it expires.
If the token you have is a request token - you would need to have this token authorized first (commonly by sending a web request to the application) and exchange it for an access token (commonly a successful response will contain the access token).
You mention that you have the token secret - so I am guessing it's an access token which will be valid as long as you are subscribed to the external applications service. So it seems like you might have all the information necessary to make an OAuth 1.0 request.
The best guide on the web for OAuth 1.0 I have come across is at -
http://hueniverse.com/oauth/guide/

Related

Get Google OAuth2 token without browser?

I am writing a service that will access a Google Nest Thermostat, and need a Google Oauth2 token in order to do so. All of the documentation I can find references a browser-driven login to identify, authenticate, and then store a token as a cookie.
All of my OAuth experience has involved receiving a secret and/or API key, and then using that against a token service to get a security token. I then use that token for subsequent API endpoints. All of the Google docs / samples tell me I have to get my "headless" service to log in via browser and get a token via a redirect, the same way I'd log in to any other service using my Google credentials. Is there a way to do this without a live browser session, i.e. just a Google endpoint that I trigger with my secret data, to get a token to use with the Google Smart Home APIs?

Quarkus Rest Client with Client Credentials Access Token

My quarkus backend is calling a rest web service which requires an access token. The access token is generated using client id, client secret and grant type client credentials. The token is valid for a couple of days.
This quarkus backend then propagates the data to an angular frontend.
I have a couple of questions:
Is there an out of the box implementation from Quarkus framework?
If not, please guide me if I should use httpclient or any other library for getting the access token.
How to check for refresh token?
How to save the access token, so that it can be used for other requests by other users?
Otherwise I end up generating an access token every time a user calls the rest service.
Since there is no answer, I will write here how I implemented this:
I use a java.net.http.httpclient to call the oauth server for getting the token with the client id and secret.
I cache the token using quarkus-cache and when the token expires, the quarkus-cache is invalidated and rebuilt with the new token.
Suggestions or better solutions are welcome.

Parse ADFS token in Java

I set up login in web application with ADFS.
Authorization request looks like:
https://sso.company.net/adfs/oauth2/authorize?response_type=code&client_id=ruleman&resource=urn:ruleman:1&redirect_uri=http://ruleman.net/authorize
ADFS performs authorization and redirects to the app:
http://ruleman.net/authorize?code=aaaaaaaa.bbbbbbbbb.ccccccccc
One knows that the token from code parameter contains claims such as username etc. How to decode the token and extract the claims?
The Postman flow for this - refer Postman : Authorisation Code Grant on Server 2016 - ADFS 4.0.
This code grant is the flow you have described.
As per the other answers:
Use the authorize endpoint
Get the code
Send the code to the token endpoint
Get the JWT
Use jwt.io to examine the JWT.
The flow follows the OAuth 2.0 standard. Please note I am not expert in ADFS, however I know OAuth 2.0 well.
The authorization flow consists of multiple options with different steps. In your case you are using the code profile (specifying response_type=code). The authorization step you did is only first step, there are a few steps to follow
you can search on "OAuth 2.0 with ADFS" e.g. http://blog.scottlogic.com/2015/03/09/OAUTH2-Authentication-with-ADFS-3.0.html
Authorization request
../authorize?response_type=code&client_id=ruleman
&resource=urn:ruleman:1&redirect_uri=http://ruleman.net/authorize
you will receive an OAuth code (usually not aving any information value, it is only a code)
http://ruleman.net/authorize?code=aaaaaaaa.bbbbbbbbb.ccccccccc
code parameter contains claims such as username etc
This is wrong assumption
Using this code you need to call a token service from backend to receive an access token (e.g. using HttpClient).
POST /adfs/oauth2/token HTTP/1.1
grant_type=authorization_code&client_id=some-uid-or-
other&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2FgetAToken&code=thecode
you will receive an access token. This step ensures you application is really authenticated with the identity provider it knows.
According to the post linked above:
The interesting bit is the itself, it is in fact a JSON Web Token (JWT). That’s to say a signed representation of the user’s identity and other grants.
I am unable to confirm that, but you can try. Usually (with other identity providers) the token is only a token and the client neeeds to call a "user information" service to get any user identity claims, however seems the ADFS gives you some shortcut.
Then you can use any JWT library to decode/validate the jwt token (com.auth0/java-jwt/3.0.1)
com.auth0.jwt.interfaces.DecodedJWT jwt = com.auth0.jwt.JWT.decode(token);

How to communicate between two java servers using public private key API token

I have two Java Spring applications, one is working as client and other as server. Client is Spring RESTful service.
My requirement is:
Once a server wants to communicate with a client, it should send some token. The token will get validated by client. If the token is valid then client performs some task and sends success result. Otherwise the response will be like not valid token and client does not perform any task for that request.
I think what you need is JWT token, you can learn JWT token and java in JWT token, using the java lib to generate token and validate.
The way to transfer token is to add custom header in request header, so you can just add a filter to intercept the request and validate the token.
If you want use authority not just a simple token you can use spring security and JWT token, the demo and reference can see REST Security with JWT using Java and Spring Security and demo.
In my project,I using spring security+ spring session rest+ hazelcast,it's also a way to protect my rest api by token.
You can chose the method you need and if any question you can comment under the answer.

Do I need an OAuth server implementation for a web application with a login system?

If I'm creating a web application with a RESTful back end, is OAuth 2.0 really necessary given that I don't want to connect with social media (Facebook, Google+, etc.)? I'm thinking about ditching OAuth2.0 and doing the following:
Generate JWT tokens upon successful login
Store this token in redis (or database, haven't decided)
Have a filter that checks for the JWT token and matches the token with the one in redis/db.
If the token exists, allow the user to access the resource
You can manually generate JWT tokens if you wish but it would be better to let an oAuth 2.0 implementation handle the JWT generation.
You don't need to persist the token. Just check the signed signature of the JWT, there is no need to check a database as the point of a JWT is for stateless authentication.
OAuth 2.0 3 legged flow is mainly used to address a third-party application to gain resource owner access without sharing resource owner's credentials with the third party application.For example, a photo print application wants to access resource owner's (user) flicker account on behalf of resource owner without sharing the resource owner flicker account credentials.
In the traditional client-server authentication, you may consider to use OAuth 2.0 2 legged resource owner grant where OAuth 2.0 client application can request OAuth 2.0 Server to create OAuth Access Token. In this case, you can use JWT Token for OAuth Access Token. This flow is almost you have mentioned but only standard OAuth 2.0 resource owner request and response. Please refer resource owner request and response details at https://www.rfc-editor.org/rfc/rfc6749#section-4.3.2. If you use standard 2 legged OAuth flow, then client and server integration will be easy and interoperable.
If you don't want to support terminate(revoke) token operation, then you don't need to store the token in the database or any other place. In this case, the token can be self-expired but not terminated.
Even if you have a requirement for terminate token, then don't store the entire token and just store token uuid (random id) and set the token uuid in one of JWT Token Claim.

Categories

Resources