Initial commit
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.ubtrobot.okhttp.interceptor.sign;
|
||||
|
||||
import com.ubt.jimu.base.http.ApiConstants;
|
||||
import java.io.IOException;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class AuthorizationInterceptor implements Interceptor {
|
||||
private final AuthenticationInfoSource a;
|
||||
|
||||
public interface AuthenticationInfoSource {
|
||||
String a(Request request);
|
||||
|
||||
String b(Request request);
|
||||
}
|
||||
|
||||
public AuthorizationInterceptor(AuthenticationInfoSource authenticationInfoSource) {
|
||||
if (authenticationInfoSource == null) {
|
||||
throw new IllegalArgumentException("Argument infoSource is null.");
|
||||
}
|
||||
this.a = authenticationInfoSource;
|
||||
}
|
||||
|
||||
@Override // okhttp3.Interceptor
|
||||
public Response intercept(Interceptor.Chain chain) throws IOException {
|
||||
Request request = chain.request();
|
||||
Request.Builder newBuilder = request.newBuilder();
|
||||
if (this.a.b(request) != null) {
|
||||
newBuilder.header("Authorization", this.a.b(request));
|
||||
}
|
||||
if (this.a.a(request) != null) {
|
||||
newBuilder.header(ApiConstants.KEY_DEVICE_ID, this.a.a(request));
|
||||
}
|
||||
return chain.proceed(newBuilder.build());
|
||||
}
|
||||
}
|
@@ -0,0 +1,99 @@
|
||||
package com.ubtrobot.okhttp.interceptor.sign;
|
||||
|
||||
import android.os.SystemClock;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
import com.ubt.jimu.base.http.ApiConstants;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class HttpSignInterceptor implements Interceptor {
|
||||
private final String a;
|
||||
private final DeviceInfoSource b;
|
||||
private final URestSigner c;
|
||||
private volatile boolean d;
|
||||
private volatile Pair<Long, Long> e;
|
||||
private OkHttpClient f;
|
||||
|
||||
public interface DeviceInfoSource {
|
||||
String a(Request request);
|
||||
}
|
||||
|
||||
public HttpSignInterceptor(String str, String str2, DeviceInfoSource deviceInfoSource) {
|
||||
if (str == null || str.length() <= 0 || str2 == null || str2.length() <= 0) {
|
||||
throw new IllegalArgumentException("appId or appKey arguments is empty.");
|
||||
}
|
||||
if (deviceInfoSource == null) {
|
||||
throw new IllegalArgumentException("Argument deviceInfoSource is null.");
|
||||
}
|
||||
this.a = str;
|
||||
this.c = new URestSigner(str2);
|
||||
this.b = deviceInfoSource;
|
||||
}
|
||||
|
||||
private long a() throws IOException {
|
||||
if (this.f == null) {
|
||||
this.f = new OkHttpClient();
|
||||
}
|
||||
ResponseBody body = this.f.newCall(new Request.Builder().url("https://apis.ubtrobot.com/v1/client-auth-service/api/timestamp").get().build()).execute().body();
|
||||
if (body == null) {
|
||||
throw new IOException("Service response empty body.");
|
||||
}
|
||||
try {
|
||||
return new JSONObject(new String(body.bytes())).getLong("timestamp");
|
||||
} catch (JSONException e) {
|
||||
throw new IOException("Service response unexpected json object.", e);
|
||||
}
|
||||
}
|
||||
|
||||
private long b() {
|
||||
return this.d ? ((Long) this.e.second).longValue() + TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - ((Long) this.e.first).longValue()) : System.currentTimeMillis() / 1000;
|
||||
}
|
||||
|
||||
private void c() {
|
||||
if (this.d) {
|
||||
return;
|
||||
}
|
||||
synchronized (this) {
|
||||
if (this.d) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
this.e = new Pair<>(Long.valueOf(SystemClock.elapsedRealtime()), Long.valueOf(a() + TimeUnit.MILLISECONDS.toSeconds((SystemClock.elapsedRealtime() - SystemClock.elapsedRealtime()) / 2)));
|
||||
this.d = true;
|
||||
this.f = null;
|
||||
} catch (IOException e) {
|
||||
Log.e("HttpSignInterceptor", "Sync time failed before request which need sign.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.Interceptor
|
||||
public Response intercept(Interceptor.Chain chain) throws IOException {
|
||||
Request request = chain.request();
|
||||
String a = this.b.a(request);
|
||||
if (TextUtils.isEmpty(a)) {
|
||||
throw new IllegalStateException("DeviceInfoSource.getDeviceId return an empty device id.");
|
||||
}
|
||||
String header = request.header(ApiConstants.KEY_DEVICE_ID);
|
||||
if (header != null && !a.equals(header)) {
|
||||
throw new IllegalStateException("X-UBT-DeviceId header value is different from " + a);
|
||||
}
|
||||
String header2 = request.header(ApiConstants.KEY_APP_ID);
|
||||
if (header2 == null || this.a.equals(header2)) {
|
||||
c();
|
||||
return chain.proceed(request.newBuilder().header(ApiConstants.KEY_APP_ID, this.a).header(ApiConstants.KEY_DEVICE_ID, a).header(ApiConstants.KEY_SIGN, this.c.a(a, b(), UUID.randomUUID().toString().substring(0, 8))).build());
|
||||
}
|
||||
throw new IllegalStateException("X-UBT-AppId header value is different from " + this.a);
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
package com.ubtrobot.okhttp.interceptor.sign;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class URestSigner {
|
||||
private final String a;
|
||||
|
||||
public URestSigner(String str) {
|
||||
this.a = str;
|
||||
}
|
||||
|
||||
public String a(String str, long j, String str2) {
|
||||
return a(j + this.a + str2 + str) + " " + j + " " + str2 + " v2";
|
||||
}
|
||||
|
||||
private String a(String str) {
|
||||
try {
|
||||
String bigInteger = new BigInteger(1, MessageDigest.getInstance("MD5").digest(str.getBytes())).toString(16);
|
||||
while (bigInteger.length() < 32) {
|
||||
bigInteger = "0" + bigInteger;
|
||||
}
|
||||
return bigInteger;
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user