Faʻaaogaina REST-mautinoa e Tofotofoga OAuth 2.0 tafe faʻataʻitaʻiga

OAuth 2.0 maua ai fa eseʻese tafe, ae o le autu faʻamoemoe o taʻitasi tafe o le maua se ulufale_token ma faʻaaogaina ia avanoa i puipuiga puipuia.

O vaitafe ese e fa o:

  • Faʻatagaina Tulafono Faʻatagaina Grant
  • Fuafua Fesoasoani Faʻatagaina
  • Tagata Faʻamaonia Faʻamaonia
  • Alu o le Upega Faʻatagaina

O lenei aʻoaʻoga faʻaavanoaina faʻataʻitaʻiga faʻataʻitaʻiga e faʻaaoga ai le REST-assured e faʻataʻitaʻi ai le tafe o le OAuth 2.0, Faʻatagaina le Faʻatagaina o Tulafono ma le Faʻamaoniga o Tagata Faʻatau

Faʻatagaina le Faʻatagaina o le Tulafono Faʻatagaina

Lenei o le sili taatele tafe pe a fai o se tulafono faʻatulafonoina e faʻailoa mai ma faʻaaogaina e maua ai le avanoa_token . O lenei tulafono o loʻo tuleia i le pito i luma-iuga talosaga (luga o le suʻesuʻe) pe a maeʻa ona saini le tagata i totonu. O le access_token e tuuina atu i le 'auʻaunaga itu, faʻamaonia le tagata o tausia ma lana password ma le mauaina code.

Tolu sitepu faʻagasologa:

  • 1 - Maua Tulafono Auth
  • 2 - Maua Avanoa Faʻailoga
  • 3 - Faʻaaoga Avanoa Faʻailoga (ia faʻaaoga puipuia puipuiga)

Maua le Tulafono Auth

O le sitepu muamua o le mauaina o le code:



import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; import java.util.Base64; public class RestAssuredOAuth2 {
public static String clientId = 'some_client_id';
public static String redirectUri = 'some_redirect_uri';
public static String scope = 'some_scope';
public static String username = 'some_email';
public static String password = 'some_password';
public static String encode(String str1, String str2) {
return new String(Base64.getEncoder().encode((str1 + ':' + str2).getBytes()));
}
public static Response getCode() {
String authorization = encode(username, password);

return


given()




.header('authorization', 'Basic ' + authorization)




.contentType(ContentType.URLENC)




.formParam('response_type', 'code')




.queryParam('client_id', clientId)




.queryParam('redirect_uri', redirectUri)




.queryParam('scope', scope)




.post('/oauth2/authorize')




.then()




.statusCode(200)




.extract()




.response();
}
public static String parseForOAuth2Code(Response response) {
return response.jsonPath().getString('code');
}
@BeforeAll
public static void setup() {
RestAssured.baseURI = 'https://some-url.com';
}
@Test
public void iShouldGetCode() {
Response response = getCode();
String code = parseForOAuth2Code(response);

Assertions.assertNotNull(code);
} }

Maua Avanoa Faʻailoga

O le taimi lava tatou te maua ai le faʻatagaga code, ona mafai loa lea ona tatou talosaga mo access_token:

public static Response getToken(String authCode) {
String authorization = encode(username, password);
return
given()

.header('authorization', 'Basic ' + authorization)

.contentType(ContentType.URLENC)

.queryParam('code', authCode)

.queryParam('redirect_uri', redirectUri)

.queryParam('grant_type', grantType)

.post('/oauth2/token')

.then()

.statusCode(200)

.extract()

.response();
}
public static String parseForAccessToken(Response loginResponse) {
return loginResponse.jsonPath().getString('access_token');
}
@Test
public void iShouldGetToken() {
Response tokenResponse = getToken(code);
String accessToken = parseForAccessToken(tokenResponse);
Assertions.assertNotNull(accessToken);
}

Faʻaaogaina Avanoa Faʻailoga

I le iuga, a i ai se matou faʻamaoniga access_token, e mafai loa ona tatou faia ni talosaga i mea puipuia:

public static void getUsers() {
given().auth()
.oauth2(accessToken)
.when()
.get('/users')
.then()
.statusCode(200); }

E mafai foʻi ona matou lafoina atu le faʻailoga o se Authorization Header ma le Bearer nauna:

Faataitaiga:

public static void getUsers() {
given()
.header('Authorization', 'Bearer ' + accessToken)
.when()
.get('/users')
.then()
.statusCode(200); }

Tafe a le Tagata Faʻatau

O le tagata faʻamaoni faʻasolosolo tafe leai UI (browser) aofia ai ma e masani ona faʻaaogaina mo masini-i-Masini faʻatagaina.

I REST-mautinoa, o lenei o le a foliga pei o:

import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; import static io.restassured.RestAssured.requestSpecification; public class RestAssuredOAuth2 {
public static Response response;
private String userAdminClientId = System.getenv('M2M_USER_ADMIN_CLIENT_ID');
private String userAdminClientSecret = System.getenv('M2M_USER_ADMIN_CLIENT_SECRET');
private String oauth2Payload = '{ ' +

' 'client_id': '' + userAdminClientId + '', ' +

' 'client_secret': '' + userAdminClientSecret + '', ' +

' 'audience': 'https://some-url.com/user', ' +

' 'grant_type': 'client_credentials', ' +

' 'scope': 'user:admin' }';
private static String createUserPayload = '{ ' +

' 'username': 'api-user', ' +

' 'email': 'api-user@putsbox.com', ' +

' 'password': 'Passw0rd123!', ' +

' 'firstName': 'my-first-name', ' +

' 'lastName': 'my-last-name', ' +

' 'roles': ['read'] }';
public void userAdminConfigSetup() {
requestSpecification = given().auth().oauth2(getAccessToken(oauth2Payload))


.header('Accept', ContentType.JSON.getAcceptHeader())


.contentType(ContentType.JSON);
}
public String getAccessToken(String payload) {
return given()


.contentType(ContentType.JSON)


.body(payload)


.post('/token')


.then().extract().response()


.jsonPath().getString('access_token');
}
@BeforeAll
public static void setup() {
RestAssured.baseURI = 'https://some-url.com';
}
@Test
public void createUser() {
userAdminConfigSetup();
response = given(requestSpecification)


.body(createUserPayload)


.post('/user')


.then().extract().response();

Assertions.assertEquals(201, response.statusCode());
} }

Faaiuga

Lenei, na matou saunia faʻailoga faʻataʻitaʻiga ma REST-mautinoa i auala e maua ai access_token faʻaaogaina o le OAuth 2.0 tafe. E maua loa le access_token e mafai ona tatou faia ni talosaga i mea puipuia.

Faamoemoe ua e maua aoga luga.