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 Two authHeader = authTuple(username, password);
        httpHeaders.set(authHeader.a, authHeader.b);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);

        return httpHeaders;
    }

    public static Two authTuple(String username, String password) {
        final byte[] encodedAuth = Base64.getEncoder().encode((username + ":" + password).getBytes());
        return Two.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