All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.afrozaar.wordpress.wpapi.v2.util.AuthUtil Maven / Gradle / Ivy

There is a newer version: 4.8.3
Show newest version
package com.afrozaar.wordpress.wpapi.v2.util;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import java.util.Base64;

public class AuthUtil {

    public static HttpHeaders createHeaders(String username, String password) {
        HttpHeaders httpHeaders = new HttpHeaders();
        final Tuple2 authHeader = authTuple(username, password);
        httpHeaders.set(authHeader.a, authHeader.b);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);

        return httpHeaders;
    }

    public static Tuple2 authTuple(String username, String password) {
        final byte[] encodedAuth = Base64.getEncoder().encode((username + ":" + password).getBytes());
        return Tuple2.of("Authorization", "Basic " + new String(encodedAuth));
    }

    public static HttpEntity basicAuth(String username, String password) {
        return new HttpEntity<>(createHeaders(username, password));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy