Authentication in RestAssured
authentication is a core part of API testing in RestAssured, and mastering it is must-have for automation testers.
Letβs go step-by-step β covering all authentication types, their usage, code examples, when to use, and common errors.
π Authentication in RestAssured β Complete Guide
Authentication means verifying the identity of the API client (you or your test).
Most APIs require authentication to ensure only authorized users can access data.
π§© 1οΈβ£ No Authentication (Open API)
π Description
Public APIs that donβt require credentials.
Example: Open weather APIs, currency converters.
β Example
given()
.when()
.get("https://api.zippopotam.us/us/90210")
.then()
.statusCode(200)
.log().all();
π‘ When to use
Open/public data endpoints
Simple testing or health check APIs
π§© 2οΈβ£ Basic Authentication
π Description
The simplest authentication type.
Sends username and password in the header (Base64 encoded).
β οΈ Not secure if used over HTTP (always use HTTPS).
β Syntax
given()
.auth()
.basic("username", "password")
.when()
.get("https://api.example.com/users")
.then()
.statusCode(200);
β Alternate Way (Preemptive)
given()
.auth()
.preemptive()
.basic("username", "password")
.when()
.get("https://api.example.com/users")
.then()
.statusCode(200);
π‘ Difference:
| Type | Description |
basic() | Server first sends 401, then client sends credentials |
preemptive().basic() | Client sends credentials in the first request itself (faster) |
π§© 3οΈβ£ Digest Authentication
π Description
More secure version of Basic Authentication.
Uses a hash of the password instead of sending it in plain text.
Common in legacy enterprise systems.
β Example
given()
.auth()
.digest("username", "password")
.when()
.get("https://api.example.com/secure/data")
.then()
.statusCode(200);
π‘ When to use
- APIs supporting Digest Authentication (older web servers or internal APIs)
π§© 4οΈβ£ Bearer Token / OAuth2 Token
π Description
The most common modern authentication.
Token is issued after login and sent with every request.
Used in most REST APIs (JWT, OAuth2, API keys).
β Example
given()
.auth()
.oauth2("your_token_here")
.when()
.get("https://api.github.com/user")
.then()
.statusCode(200);
Or with Header manually:
given()
.header("Authorization", "Bearer your_token_here")
.when()
.get("https://api.github.com/user")
.then()
.statusCode(200);
π‘ When to use
- APIs using JWT, OAuth2, or token-based sessions (GitHub, Google, Jira, etc.)
π§© 5οΈβ£ API Key Authentication
π Description
Common for public APIs where a single key identifies the client.
Key is sent as a query param or header.
β Example (Header)
given()
.header("x-api-key", "12345abcde")
.when()
.get("https://api.weatherapi.com/v1/current.json?q=London")
.then()
.statusCode(200);
β Example (Query Param)
given()
.queryParam("appid", "your_api_key")
.queryParam("q", "London")
.when()
.get("https://api.openweathermap.org/data/2.5/weather")
.then()
.statusCode(200);
π‘ When to use
- External or third-party APIs (Google Maps, OpenWeather, NewsAPI, etc.)
π§© 6οΈβ£ Form Authentication
π Description
Used when the application authenticates using a login form (HTML form submission).
RestAssured can simulate form-based login.
β Example
given()
.auth()
.form("username", "password", new FormAuthConfig("/login", "uname", "pwd"))
.when()
.get("https://app.example.com/home")
.then()
.statusCode(200);
π‘ Parameters:
| Argument | Description |
/login | Login endpoint |
uname | Username field name |
pwd | Password field name |
π§© 7οΈβ£ OAuth 1.0
π Description
Older version of OAuth (used by Twitter API v1).
Uses consumer key, secret, token, and token secret.
β Example
given()
.auth()
.oauth("consumerKey", "consumerSecret", "accessToken", "secretToken")
.when()
.get("https://api.twitter.com/1.1/statuses/home_timeline.json")
.then()
.statusCode(200);
π‘ When to use
- Only when working with legacy APIs (Twitter API v1.1, some banking APIs)
π§© 8οΈβ£ OAuth 2.0 (Access Token Flow)
π Description
The modern standard for authentication (used by Google, GitHub, Azure, etc.)
Involves:
Getting access token using client credentials.
Using token in further requests.
β Step 1: Get Token
Response tokenResponse = given()
.formParam("client_id", "myClientId")
.formParam("client_secret", "mySecret")
.formParam("grant_type", "client_credentials")
.when()
.post("https://authserver.com/oauth/token");
String accessToken = tokenResponse.jsonPath().getString("access_token");
β Step 2: Use Token
given()
.header("Authorization", "Bearer " + accessToken)
.when()
.get("https://api.example.com/resource")
.then()
.statusCode(200);
π§© 9οΈβ£ Session / Cookie Based Authentication
π Description
Used in traditional web apps.
You first log in β receive a session cookie β use it for later requests.
β Example
Response res = given()
.formParam("username", "admin")
.formParam("password", "admin123")
.when()
.post("https://app.example.com/login");
String sessionId = res.getCookie("JSESSIONID");
given()
.cookie("JSESSIONID", sessionId)
.when()
.get("https://app.example.com/dashboard")
.then()
.statusCode(200);
π§ 10οΈβ£ Summary Table
| Type | Security Level | Use Case | Example |
| No Auth | β | Open/Public APIs | Public weather APIs |
| Basic | β οΈ Low | Legacy systems | Internal APIs |
| Digest | β Medium | Enterprise | Internal/secure APIs |
| Bearer/OAuth2 | π High | Modern APIs | GitHub, Google, Jira |
| API Key | β Medium | Third-party | OpenWeather, NewsAPI |
| Form Auth | β οΈ Low | Web apps | Login forms |
| OAuth1.0 | β Medium | Legacy OAuth | Twitter v1 |
| Session/Cookie | β οΈ Medium | Web portals | Old-style logins |
βοΈ 11οΈβ£ Best Practices
β
Always use HTTPS (never send credentials over HTTP)
β
Use Config/Properties file for credentials, not hardcoded values
β
Use token refresh logic for long-running tests
β
Parameterize auth tokens for CI/CD (Jenkins secrets or environment variables)
β
Add utility methods for token retrieval and reuse