在Android中使用翻新的Hawk身份验证

时间:2020-04-29 08:36:06

标签: android authentication retrofit2 okhttp

我正在构建一个对api使用Hawk身份验证的应用程序。我正在使用改型在应用程序中实现api。我发现的少数几本说明如何在改型中使用hawk auth的文章之一是https://futurestud.io/tutorials/retrofit-2-hawk-authentication-on-android。本文提到了一个在任何地方都找不到的 HawkAuthenticationInterceptor 类。是否还有其他方法可以通过改装来添加Hawk授权标头。

更新:我目前正在单独的线程上计算hawk身份验证,并通过@Authorization标头添加它。另外,在当前实现中,鹰式凭据基于时间戳,并使用 Hawk.calculateMAC 。最终的授权标头包含hawkId,timestamp,nonce和mac。

例如:Hawk id =“ hawkId”,ts =“ 15912131366”,nonce =“ CQMZ4B”,mac =“ W0n / pffeysCnj20OEk4WoeO86W5r2Pi9ZgjR2kre6SU =”

1 个答案:

答案 0 :(得分:0)

Tauntz on Reddit引用了以下解决方案

package br.com.xplore.xploreretrofit1.interceptors;

import com.wealdtech.hawk.HawkClient;
import com.wealdtech.hawk.HawkCredentials;

import java.io.IOException;
import java.net.URI;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by r028367 on 29/11/2017.
 */

public class HawkAuthenticationInterceptor implements Interceptor{

    private HawkClient hawkClient;

    public HawkAuthenticationInterceptor(HawkCredentials hawkCredentials) {
        this.hawkClient = new HawkClient.Builder()
                .credentials(hawkCredentials).build();

    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        URI uri = original.url().uri();
        String method = original.method();
        String header = hawkClient
                .generateAuthorizationHeader(uri, method, null
                        , null, null, null);
        Request.Builder requestBuilder = original.newBuilder()
                .header("Authorization", header)
                .method(method, original.body());
        return chain.proceed(requestBuilder.build());
    }
}

https://github.com/chrislucas/xplore-retrofit-android/blob/master/XploreRetrofit1/app/src/main/java/br/com/xplore/xploreretrofit1/interceptors/HawkAuthenticationInterceptor.java

相关问题