Snowflake ODBC refresh token is not issued - oauth-2.0

I'm making continuous API calls using snowflake ODBC connection. My access token expires in 10 mins and able to refresh using the refresh token using the below call. But after 90 days my refresh token is getting expired. but this API endpoint
curl -X POST -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" \
--user "<OAUTH_CLIENT_ID not encoded>:<OAUTH_CLIENT_SECRET>" \
--data-urlencode "grant_type=refresh_token" \
--data-urlencode "refresh_token=<refresh_token value>" \
--data-urlencode "redirect_uri=<OAUTH_REDIRECT_URI not encoded>" \
<https://mydomain.snowflakecomputing.com/oauth/token-request>
This call returns the access token, only when the refresh token is valid.
This returns 400 bad request and not sending new access and refresh tokens when refresh token is expired.
How do we automatically refresh refresh_token.
Is possible or should we repeat the login process and thats the only way?

There is no way to refresh a refresh_token. Once the refresh_token is expired, you will have to start the whole OAuth authorization code flow, this is based on OAuth 2.0 RFC.
Login with your username/password
Get the authorization code
Exchange the access token and refresh token using the authorization code from Step (2) above
When refresh token expired, repeat Step (1)
Snowflake OAuth is based on OAuth 2.0, you may refer to the RFC here:
https://datatracker.ietf.org/doc/html/rfc6749

Related

is it necessary to pass the device id when refresh the oauth 2.0 access token

When I read the instagram refresh token api, the request look like this:
curl -i -X GET "https://graph.instagram.com/refresh_access_token
?grant_type=ig_refresh_token
&access_token={long-lived-access-token}"
my question is, why did not pass the device id to the server side, if pass the device id, the server will check the device related with the token. the oauth 2.0 told that the server side would maintain the relationship about device id and the refresh token. where do I use the relationship? I think the refresh token should use the mechanism to keep more secure.

OAuth 2 Requesting a new Access Token uses up Refresh Token?

Quick question, couldn't find the answer here. When using the refresh token to create a new access token, will this use up the refresh token?
I had an instance where I used up the refresh token to get a new Access Token however the new access token didn't come with a new refresh token.
Does this mean that I can keep using the initial refresh token every time the access token runs out?
The Google documentation says,
refresh_token: A token that may be used to obtain a new access token. Refresh tokens are valid until the user revokes access.
Refresh tokens are valid until they are revoked, so you can use it as many times as you want if it is valid.
The following is an example of refreshing an access_token with Google OAuth.
Request
POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
refresh_token=1/6BMfW9j53gdGImsiyUH5kU5RsR4zwI9lUVX-tqf8JXQ&
grant_type=refresh_token
Response
{
"access_token":"1/fFBGRNJru1FQd44AzqT3Zg",
"expires_in":3920,
"token_type":"Bearer",
}
to conclude, refresh tokens are not expired with an access token refresh. they can be used as long as they are valid.
The behavior depends on OAuth 2.0 implementations. Google's implementation is just one example. As a matter of fact, there exists an implementation that allows an administrator to select either (1) to keep a refresh token valid after use or (2) to invalidate a refresh token after use and issue a new one.

What is the OAuth 2.0 Bearer Token exactly?

According to RFC6750-The OAuth 2.0 Authorization Framework: Bearer Token Usage, the bearer token is:
A security token with the property that any party in possession of the token (a "bearer") can use the token in any way that any other party in possession of it can.
To me this definition is vague and I can't find any specification.
Suppose I am implementing an authorization provider, can I supply any kind of string for the bearer token?
Can it be a random string?
Does it have to be a base64 encoding of some attributes?
Should it be hashed?
And does the service provider need to query the authorization provider in order to validate this token?
Bearer Token
A security token with the property that any party in possession of
the token (a "bearer") can use the token in any way that any other
party in possession of it can. Using a bearer token does not
require a bearer to prove possession of cryptographic key material
(proof-of-possession).
The Bearer Token is created for you by the Authentication server. When a user authenticates your application (client) the authentication server then goes and generates for you a Token. Bearer Tokens are the predominant type of access token used with OAuth 2.0. A Bearer token basically says "Give the bearer of this token access".
The Bearer Token is normally some kind of opaque value created by the authentication server. It isn't random; it is created based upon the user giving you access and the client your application getting access.
In order to access an API for example you need to use an Access Token. Access tokens are short lived (around an hour). You use the bearer token to get a new Access token. To get an access token you send the Authentication server this bearer token along with your client id. This way the server knows that the application using the bearer token is the same application that the bearer token was created for. Example: I can't just take a bearer token created for your application and use it with my application it wont work because it wasn't generated for me.
Google Refresh token looks something like this: 1/mZ1edKKACtPAb7zGlwSzvs72PvhAbGmB8K1ZrGxpcNM
copied from comment: I don't think there are any restrictions on the bearer tokens you supply. Only thing I can think of is that its nice to allow more than one. For example a user can authenticate the application up to 30 times and the old bearer tokens will still work. oh and if one hasn't been used for say 6 months I would remove it from your system. It's your authentication server that will have to generate them and validate them so how it's formatted is up to you.
Update:
A Bearer Token is set in the Authorization header of every Inline Action HTTP Request. For example:
POST /rsvp?eventId=123 HTTP/1.1
Host: events-organizer.com
Authorization: Bearer AbCdEf123456
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/1.0 (KHTML, like Gecko; Gmail Actions)
rsvpStatus=YES
The string "AbCdEf123456" in the example above is the bearer authorization token. This is a cryptographic token produced by the authentication server. All bearer tokens sent with actions have the issue field, with the audience field specifying the sender domain as a URL of the form https://. For example, if the email is from noreply#example.com, the audience is https://example.com.
If using bearer tokens, verify that the request is coming from the authentication server and is intended for the the sender domain. If the token doesn't verify, the service should respond to the request with an HTTP response code 401 (Unauthorized).
Bearer Tokens are part of the OAuth V2 standard and widely adopted by many APIs.
As I read your question, I have tried without success to search on the Internet how Bearer tokens are encrypted or signed. I guess bearer tokens are not hashed (maybe partially, but not completely) because in that case, it will not be possible to decrypt it and retrieve users properties from it.
But your question seems to be trying to find answers on Bearer token functionality:
Suppose I am implementing an authorization provider, can I supply any
kind of string for the bearer token? Can it be a random string? Does
it has to be a base64 encoding of some attributes? Should it be
hashed?
So, I'll try to explain how Bearer tokens and Refresh tokens work:
When user requests to the server for a token sending user and password through SSL, the server returns two things: an Access token and a Refresh token.
An Access token is a Bearer token that you will have to add in all request headers to be authenticated as a concrete user.
Authorization: Bearer <access_token>
An Access token is an encrypted string with all User properties, Claims and Roles that you wish. (You can check that the size of a token increases if you add more roles or claims).
Once the Resource Server receives an access token, it will be able to decrypt it and read these user properties. This way, the user will be validated and granted along with all the application.
Access tokens have a short expiration (ie. 30 minutes).
If access tokens had a long expiration it would be a problem, because theoretically there is no possibility to revoke it. So imagine a user with a role="Admin" that changes to "User". If a user keeps the old token with role="Admin" he will be able to access till the token expiration with Admin rights.
That's why access tokens have a short expiration.
But, one issue comes in mind. If an access token has short expiration, we have to send every short period the user and password. Is this secure? No, it isn't. We should avoid it. That's when Refresh tokens appear to solve this problem.
Refresh tokens are stored in DB and will have long expiration (example: 1 month).
A user can get a new Access token (when it expires, every 30 minutes for example) using a refresh token, that the user had received in the first request for a token.
When an access token expires, the client must send a refresh token. If this refresh token exists in DB, the server will return to the client a new access token and another refresh token (and will replace the old refresh token by the new one).
In case a user Access token has been compromised, the refresh token of that user must be deleted from DB. This way the token will be valid only till the access token expires because when the hacker tries to get a new access token sending the refresh token, this action will be denied.
Bearer token is one or more repetition of alphabet, digit, "-" , "." , "_" , "~" , "+" , "/" followed by 0 or more "=".
RFC 6750 2.1. Authorization Request Header Field (Format is ABNF (Augmented BNF))
The syntax for Bearer credentials is as follows:
b64token = 1*( ALPHA / DIGIT /
"-" / "." / "_" / "~" / "+" / "/" ) *"="
credentials = "Bearer" 1*SP b64token
It looks like Base64 but according to Should the token in the header be base64 encoded?, it is not.
Digging a bit deeper in to "HTTP/1.1, part 7: Authentication"**,
however, I see that b64token is just an ABNF syntax definition
allowing for characters typically used in base64, base64url, etc.. So
the b64token doesn't define any encoding or decoding but rather just
defines what characters can be used in the part of the Authorization
header that will contain the access token.
This fully addresses the first 3 items in the OP question's list. So I'm extending this answer to address the 4th question, about whether the token must be validated, so #mon feel free to remove or edit:
The authorizer is responsible for accepting or rejecting the http request. If the authorizer says the token is valid, it's up to you to decide what this means:
Does the authorizer have a way of inspecting the URL, identifying the operation, and looking up some role-based access control database to see if it is allowed? If yes and the request comes through, the service can assume it is allowed, and does not need to verify.
Is the token an all-or-nothing, so if the token is correct, all operations are allowed? Then the service doesn't need to verify.
Does the token mean "this request is allowed, but here is the UUID for the role, you check whether the operation is allowed". Then it's up to the service to look up that role, and see if the operation is allowed.
References
RFC 5234 3.6. Variable Repetition: *Rule
RFC 2616 2.1 Augmented BNF
Please read the example in rfc6749 sec 7.1 first.
The bearer token is a type of access token, which does NOT require PoP(proof-of-possession) mechanism.
PoP means kind of multi-factor authentication to make access token more secure. ref
Proof-of-Possession refers to Cryptographic methods that mitigate the risk of Security Tokens being stolen and used by an attacker. In contrast to 'Bearer Tokens', where mere possession of the Security Token allows the attacker to use it, a PoP Security Token cannot be so easily used - the attacker MUST have both the token itself and access to some key associated with the token (which is why they are sometimes referred to 'Holder-of-Key' (HoK) tokens).
Maybe it's not the case, but I would say,
access token = payment methods
bearer token = cash
access token with PoP mechanism = credit card (signature or password will be verified, sometimes need to show your ID to match the name on the card)
BTW, there's a draft of "OAuth 2.0 Proof-of-Possession (PoP) Security Architecture" now.
A bearer token is like a currency note e.g 100$ bill . One can use the currency note without being asked any/many questions.
Bearer Token A security token with the property that any party in
possession of the token (a "bearer") can use the token in any way that
any other party in possession of it can. Using a bearer token does not
require a bearer to prove possession of cryptographic key material
(proof-of-possession).
The bearer token is a b64token string, with the requirement that if you have it, you can use it. There are no guarantees as to what the meaning of that string actually is in the specification beyond that. It is up to the implementation.
5.2. Threat Mitigation
This document does not specify the encoding or the contents of the
token; hence, detailed recommendations about the means of
guaranteeing token integrity protection are outside the scope of this
document. The token integrity protection MUST be sufficient to
prevent the token from being modified.
https://datatracker.ietf.org/doc/html/rfc6750#section-5.2
While the token could be random each time it is issued, the downside is the server side would need to keep track of the tokens data (e.g. expiration). A JSON Web Token (JWT) is often used as a bearer token, because the server can make decisions based on whats inside the token.
JWT:
https://jwt.io/

Google Oauth "Service Account", how to refresh token?

I am using Oauth to access Google Cloud Storage via their JSON API.
All is fine, I authenticate and get an access token which has an expiration of 3600.
What is the correct way to refresh this?
It is my understanding that in other types of oAuth flows (i.e. Web Server), the initial authorization request returns a refresh token as well as an access token, and that the refresh token is used to ask for another access token when the current access token has expired.
But is appears that there is no refresh token when doing server-to-server oAuth with a Google "Service Account"?
Found the answer.
https://developers.google.com/accounts/docs/OAuth2ServiceAccount#expiration
Access tokens issued by the Google OAuth 2.0 Authorization Server
expire one hour after they are issued. When an access token expires,
then the application should generate another JWT, sign it, and request
another access token.

How to unauthorize/revoke LinkedIn token in my application

I have a grails application in which I want a user to grant me access to his/her LinkedIn account to get information and show it in different ways.
I was able to do the following:
Get the authorization code
Use that authorization code to get the Access Token
I store that Access Token together with the expiration date in my User entity.
Refresh that Access Token when the expiration date is within X days from today.
Now the issue I'm having is that I would like the user to revoke or invalidate that token so that someone else can use the same computer and session and login to a different LinkedIn account.
Is this possible?
If not? Is there a way to delete the LinkedIn cookies? so that the user's LinkedIn session is finished and by being logged out from LinkedIn then they will have to grant access to my application again.
For reference:
I'm using Grails 2.1.1
I'm NOT using oauth to do the authentication, I just use the HTTPClient from Groovy to do plain and simple GET and POST requests.
Thanks a lot in advance!
I have managed to revoke token on API v2 with this call:
curl --request POST \
--url https://www.linkedin.com/oauth/v2/revoke \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data client_id=CLIENT_ID_HERE \
--data client_secret=CLIENT_SECRET_HERE \
--data token=YOUR_TOKEN_HERE
The easiest thing to do is just to delete the Access Token from your storage. This way you no longer have access to that account. When LinkedIn was using OAuth 1.0a, they had an Invalidate call which would invalidate the Access Token. But when they moved to OAuth 2.0, that went away.

Resources