A guide on how to implement an OAuth2 flow for authenticating to our REST APIs.
Overview
This guide aims to provide the basic understanding and ability to authenticate a Service Account for REST API integrations. Core concepts about the OAuth2 authentication flow are briefly explained, and an example implementation is provided. The implementation is tested using the returned access token to send a request to the REST API to list all available spaces.
Our OAuth2 implementation is based on the RFC7523 specification
Prerequisites
A Service Account must be created in the organization before continuing.
Example
The example code in this page is provided as is, it may not work in your environment and should be used as a quick guide for implementation rather than code to be used in a production environment.
Environment Setup
If you wish to run the code locally, make sure you have a working runtime environment.
The following packages are required by the example code and must be installed.
pipinstallpyjwtrequests
The following packages are required by the example code and must be installed.
npminstalljsonwebtokennpminstallaxios
The following packages are required by the example code and must be installed.
goget-ugithub.com/golang-jwt/jwt/v4
Source code
If you wish to run the code locally, make sure you have a working runtime environment.
The following packages are required by the example code and must be installed.
import timeimport jwt # pip install pyjwtimport requests # pip install requeststoken_endpoint ='https://app.neowit.io/api/auth/oauth/token'# Authentication details used in the OAuth2 flow. Should# be stored outside the application.service_account_id ='<your service account id>'service_account_key_id ='<your service account key id>'service_account_secret ='<your service account secret>'defget_access_token(account_id,key_id,secret):# Construct the JWT header. jwt_headers ={'alg':'HS256','kid': key_id,}# Construct the JWT payload. jwt_payload ={'iat':int(time.time()),# current unixtime'exp':int(time.time())+3600,# expiration unixtime'aud': token_endpoint,'iss': account_id,}# Sign and encode JWT with the secret. encoded_jwt = jwt.encode( payload=jwt_payload, key=secret, algorithm='HS256', headers=jwt_headers, )# Prepare HTTP POST request data.# note: The requests package applies Form URL-Encoding by default. data ={'assertion': encoded_jwt,'grant_type':'urn:ietf:params:oauth:grant-type:jwt-bearer'}# Exchange the JWT for an access token. response = requests.post( url=token_endpoint, headers={'Content-Type': 'application/x-www-form-urlencoded'}, data=data, )# Halt if response contains an error.if response.status_code !=200:print('Status Code: {}'.format(response.status_code))print(response.json())returnNone# Return the access token in the request.return response.json()['accessToken']defmain():# Get an access token using an OAuth2 authentication flow. access_token =get_access_token( service_account_id, service_account_key_id, service_account_secret, )# Verify that we got a valid token back.if access_token isNone:return# Test the token by sending a GET request for a list of spaces.print(requests.get( url='https://app.neowit.io/api/space/v1/space', headers={'Authorization': 'Bearer '+ access_token}, ).json())if__name__=='__main__':main()
constjwt=require('jsonwebtoken'); // npm install jsonwebtokenconstaxios=require('axios').default; // npm install axios// Authentication details used in the OAuth2 flow.consttokenEndpoint='https://app.neowit.io/api/auth/oauth/token';// Auth details, should be store outside of this appconstserviceAccountID='<your service account id>';constserviceAccountKeyID='<your service account key id>';constserviceAccountSecret='<your service account key secret>';// Creates a JWT from the arguments, and exchanges it for an // access token which is returned as a promise. If an error // occurs at any point, an error is thrown and the returned // promise is rejected.asyncfunctiongetAccessToken(accountID, keyID, secret) {// Construct the JWT header.constjwtHeaders= {'alg':'HS256','kid': keyID, };// Construct the JWT payload.constjwtPayload= {'iat':Math.floor(Date.now() /1000),// current unixtime'exp':Math.floor(Date.now() /1000) +3600,// expiration unixtime'aud': tokenEndpoint,'iss': accountID, };// Sign and encode JWT with the secret.constjwtEncoded=jwt.sign( jwtPayload, secret, { header: jwtHeaders, algorithm:'HS256', }, );// Prepare POST request data.constrequestObject= {'assertion': jwtEncoded,'grant_type':'urn:ietf:params:oauth:grant-type:jwt-bearer', };// Convert to form-urlencodedconstbody=Object.keys(requestObject).map(function(key) {returnencodeURIComponent(key) +'='+encodeURIComponent(requestObject[key]); }).join('&');// Exchange JWT for access token.constresponse=awaitaxios({ method:'POST', url: tokenEndpoint, headers: { 'Content-Type':'application/x-www-form-urlencoded' }, data: requestData, }).catch(function(error) {// Prints the error response (if any), an re-throws the error.if (error.response) {console.log(error.response.data); }throw error; });// Return the access token in the request.returnresponse.data.accessToken;}asyncfunctionmain() {// Get an access token using an OAuth2 authentication flow.constaccessToken=awaitgetAccessToken( serviceAccountID, serviceAccountKeyID, serviceAccountSecret, );// Test the token by sending a GET request for a list of spaces.constresponse=awaitaxios({ method:'GET', url:'https://app.neowit.io/api/space/v1/space', headers: { 'Authorization':'Bearer '+ accessToken }, });// Print response data.console.log(JSON.stringify(response.data,null,2));}main();
packagemainimport ("context""encoding/json""errors""fmt""log""net/http""net/url""os""strings""time" jwt "github.com/golang-jwt/jwt/v4"// go get -u github.com/golang-jwt/jwt/v4@v4.4.2)const (// Used to exchange a JWT for an access token. tokenEndpoint ='https://app.neowit.io/api/auth/oauth/token';// Base URL for the REST API. apiBaseUrl ="https://app.neowit.io/api"// auth details, should be store outside of this app serviceAccountID ='<your service account id>'; serviceAccountKeyID ='<your service account key id>'; serviceAccountSecret ='<your service account key secret>';)typeAuthResponsestruct {// The access token used to access REST API. AccessToken string`json:"accessToken"`}var client = http.DefaultClientfuncgetAccessToken(ctx context.Context, accountID, keyID, secret string) (*AuthResponse, error) {// set a reasonable timeout for the request ctx, cancel := context.WithTimeout(ctx, time.Second *3)defercancel()// Construct the JWT header. jwtHeader :=map[string]interface{}{"alg": "HS256","kid": keyID, }// Construct the JWT payload. now := time.Now() jwtPayload :=&jwt.RegisteredClaims{ Issuer: accountID, Audience: jwt.ClaimStrings{tokenEndpoint}, IssuedAt: jwt.NewNumericDate(now), ExpiresAt: jwt.NewNumericDate(now.Add(time.Hour)), }// Sign and encode JWT with the secret. token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwtPayload) token.Header = jwtHeader encodedJwt, err := token.SignedString([]byte(secret))if err !=nil {returnnil, fmt.Errorf("Failed to sign jwt: %w", err) }// Prepare HTTP POST request data.// NOTE: The body must be Form URL-Encoded. body :=url.Values{"assertion": {encodedJwt},"grant_type": {"urn:ietf:params:oauth:grant-type:jwt-bearer"}, }.Encode()// Create the request to exchange the JWT for an access token. req, err := http.NewRequestWithContext( ctx, http.MethodPost, tokenEndpoint, strings.NewReader(body), )if err !=nil {returnnil, fmt.Errorf("Failed to create request: %w", err) }// Set Content-Type header to specify that our body is Form-URL Encoded. req.Header.Set("Content-Type", "application/x-www-form-urlencoded") res, err := client.Do(req)if err !=nil {returnnil, fmt.Errorf("Failed to perform request: %w", err) }defer res.Body.Close()if res.StatusCode != http.StatusOK {returnnil, fmt.Errorf("Invalid status code: %d", res.StatusCode) }// Decode the response body to an AuthResponse.var authResponse AuthResponseif err := json.NewDecoder(res.Body).Decode(&authResponse); err !=nil {returnnil, fmt.Errorf("Failed to decode response: %w", err) }// Return the AuthResponse, which contains the access token.return&authResponse, nil}funclistSpaces(ctx context.Context, auth *AuthResponse) error {// set a timeout for the request ctx, cancel := context.WithTimeout(ctx, time.Second *3)defercancel()// Create the request to get a list of spaces req, err := http.NewRequestWithContext( ctx, http.MethodGet, apiBaseUrl+"/space/v1/space",nil, )if err !=nil {return fmt.Errorf(""Failed to create request: %w", err) } // Set the Authorization header req.Header.Set("Authorization", "Bearer " + auth.AccessToken) // Send the GET request to list all spaces. resp, err := client.Do(req) if err != nil { return fmt.Errorf("Failed to list spaces: %w", err) } defer resp.Body.Close() // create a structure to decode the response response := make(map[string]interface{}) if err = json.NewDecoder(resp.Body).Decode(&projectsResponse); err != nil { return fmt.Errorf("Failed to decode response: %w", err) } fmt.Printf("GOT RESPONSE: %#v\n", response) return nil}func main() { ctx, cancel := context.WithTimeout(context.TODO(), time.Second*10) defer cancel() // OAuth2 authentication flow. auth, err := getAccessToken( ctx, serviceAccountID, serviceAccountKeyID, serviceAccountSecret, ) if err != nil { log.Fatal(err) } // Test the access token by listing all the spaces if err := listSpaces(ctx, auth); err != nil { log.Fatal(err) }}
Code walkthrough
Authenticating a client is a 3 step process which can be summarized by the following points. Each step will be detailed more below.
A JWT is constructed and signed with a secret.
The JWT is exchanged for an Access Token.
The Access Token is used to authenticate with the REST API.
1. Create the JWT
In general, a JWT contains three fields:
Header: Token type and signature algorithm.
Payload: Claims and additional data.
Signature: A signature calculated of the entire JWT, using a private secret.
Before being sent, these fields are each Base64Url encoded. They are combined in a compact dot format in the form Base64Url(header).Base64Url(payload).Base64Url(signature), which is what we will refer to as the encoded JWT.
This guide will not cover much more details of JWT. For a great introduction on JWT that provides an interactive editor and an exhaustive list of client libraries, please see jwt.io.
Using your Service Account credentials, construct the JWT headers and payload. Here, iat is the issuing time, and exp the expiration time of a maximum 1 hour after iat.
# Inputstoken_endpoint ='https://app.neowit.io/api/auth/oauth/token'key_id ='<service account key id>'account_id ='<service account id>'# Construct the JWT header.jwt_headers ={'alg':'HS256','kid': key_id,}# Construct the JWT payload.jwt_payload ={'iat':int(time.time()),# current unixtime'exp':int(time.time())+3600,# expiration unixtime'aud': account_id,'iss': token_endpoint,}
// InputsconsttokenEndpoint='https://app.neowit.io/api/auth/oauth/token';constkeyID='<service account key id>';constaccountID='<service account id>';// Construct the JWT header.constjwtHeaders= {'alg':'HS256','kid': keyID,};// Construct the JWT payload.constjwtPayload= {'iat':Math.floor(Date.now() /1000),// current unixtime'exp':Math.floor(Date.now() /1000) +3600,// expiration unixtime'aud': tokenEndpoint,'iss': accountID,};
The simplest way of Base64-encoding and signing our JWT is to use some language-specific library. This is available in most languages but can be done manually if desired.
# Inputssecret ='<service account secret>'# Sign and encode JWT with the secret.encoded_jwt = jwt.encode( payload = jwt_payload, key = secret, algorithm ='HS256', headers = jwt_headers,)
// Inputsconstsecret='<service account secret>';// Sign and encode JWT with the secret.constjwtEncoded=jwt.sign( jwtPayload, secret, { header: jwtHeaders, algorithm:'HS256', },);
// Inputsconst secret ="<service account secret>"// Sign and encode JWT with the secrettoken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwtPayload)token.Header = jwtHeaderencodedJwt, err := token.SignedString([]byte(secret))
2. Exchange for Access Token
The encoded JWT is exchanged for an Access Token by sending a POST request to the same endpoint used to construct the JWT, namely https://app.neowit.io/api/auth/oauth/token.
The POST request header should include a Content-Type field indicating the format of the body. Additionally, the POST request body is Form URL-Encoded and contains the following fields:
"assertion" - Contains the encoded JWT string.
"grant_type" - Contains the string "urn:ietf:params:oauth:grant-type:jwt-bearer". This specifies that you want to exchange a JWT for an Access Token.
It is important to note that the data has to be Form URL-Encoded. Like Python's requests, some libraries do this by default and require no further input by the user. This is, however, not the norm and likely requires an additional step before sending the request. The URL Form Encoded data should have the following format.
The header, payload, and signature are found in the previous step.
# Inputstoken_endpoint ='https://app.neowit.io/api/auth/oauth/token'# Prepare HTTP POST request data.# Note: The requests package applies Form URL-Encoding by default.request_data ={'assertion': encoded_jwt,'grant_type':'urn:ietf:params:oauth:grant-type:jwt-bearer',}# Exchange the JWT for an access token.response = requests.post( url = token_endpoint, headers = { 'Content-Type': 'application/x-www-form-urlencoded' }, data = request_data,)
// InputsconsttokenEndpoint='https://app.neowit.io/api/auth/oauth/token';// Prepare POST request data.constrequestObject= {'assertion': jwtEncoded,'grant_type':'urn:ietf:params:oauth:grant-type:jwt-bearer',};// Converts the requestObject to a Form URL-Encoded string.constbody=Object.keys(requestObject).map(function(key) {returnencodeURIComponent(key) +'='+encodeURIComponent(requestObject[key])}).join('&');// Exchange JWT for access token.constresponse=awaitaxios({ method:'POST', url: tokenEndpoint, headers: { 'Content-Type':'application/x-www-form-urlencoded' }, data: body,}).catch(function (error) {if (error.response) {console.log(error.response.data); }throw error;});
// Inputsconst tokenEndpoint ="https://app.neowit.io/api/auth/oauth/token"// Prepare HTTP POST request data.// NOTE: The body must be Form URL-Encodedbody :=url.Values{"assertion": {encodedJwt},"grant_type": {"urn:ietf:params:oauth:grant-type:jwt-bearer"},}.Encode()// Create the request to exchange the JWT for an access tokenreq, err := http.NewRequestWithContext( ctx, http.MethodPost, tokenEndpoint, strings.NewReader(body),)if err !=nil {returnnil, err}// Set Content-Type header to specify that our body// is Form-URL Encodedreq.Header.Set("Content-Type", "application/x-www-form-urlencoded")// Exchange the JWT for an access token.res, err := client.Do(req)if err !=nil {returnnil, err}defer res.Body.Close()
The Access token response will have the following format and will be valid for 1 hour. To refresh the token, repeat the above steps.
{"accessToken":"<access token for use in the Authorization header>"}
3. Access The REST API
Once you have the Access Token, you need to include this with every call to the API. This can be achieved by including the Authorization header in the form shown in the snippet below.
# Test the token by sending a GET request for a list of spaces.print(requests.get( url ='https://app.neowit.io/api/space/v1/space', headers = {'Authorization': 'Bearer '+ access_token},).json())
// Test the token by sending a GET request for a list of spaces.constresponse=awaitaxios({ method:'GET', url:'https://app.neowit.io/api/space/v1/space', headers: { 'Authorization':'Bearer '+ accessToken },});
// Test the access token by listing all the spacesif err :=listSpaces(ctx, auth); err !=nil { log.Fatal(err)}
The Access token response will have the following format and will be valid for 1 hour. To refresh the token, repeat the above steps.
Refreshing the Access Token
The access token retrieved using the process above needs to be refreshed every hour. This may be done by intercepting the 401 http response code, or by checking if the expiry time is getting close before performing each REST API call.