|
@@ -1,22 +1,33 @@
|
|
|
package com.adx.tencent.oppo.service;
|
|
package com.adx.tencent.oppo.service;
|
|
|
|
|
|
|
|
-import com.adx.tencent.oppo.model.OppoTokenRecord;
|
|
|
|
|
-import com.adx.tencent.oppo.store.OppoColdStore;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.security.MessageDigest;
|
|
import java.security.MessageDigest;
|
|
|
import java.time.Instant;
|
|
import java.time.Instant;
|
|
|
import java.util.Base64;
|
|
import java.util.Base64;
|
|
|
|
|
|
|
|
-/** OPPO ownerToken 生成与持久化;token 有效期按文档的 20 分钟窗口处理。 */
|
|
|
|
|
|
|
+/** OPPO ownerToken 本地生成服务。 */
|
|
|
public class OppoAuthService {
|
|
public class OppoAuthService {
|
|
|
- private final OppoColdStore store;
|
|
|
|
|
- public OppoAuthService(OppoColdStore store){this.store=store;}
|
|
|
|
|
- public synchronized String getOwnerToken(String ownerId,String apiId,String apiKey) {
|
|
|
|
|
- if(ownerId==null||ownerId.isBlank()||apiId==null||apiId.isBlank()||apiKey==null||apiKey.isBlank()) throw new IllegalStateException("OPPO ownerId/apiId/apiKey are required");
|
|
|
|
|
- long now=Instant.now().getEpochSecond(); OppoTokenRecord old=store.getToken(ownerId);
|
|
|
|
|
- if(old!=null&&old.getOwnerToken()!=null&&old.getIssuedAt()!=null&&Math.abs(now-old.getIssuedAt())<900)return old.getOwnerToken();
|
|
|
|
|
- String sign=sha1(apiId+apiKey+now); String token=Base64.getEncoder().encodeToString((ownerId+","+apiId+","+now+","+sign).getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
- OppoTokenRecord record=new OppoTokenRecord();record.setOwnerId(ownerId);record.setApiId(apiId);record.setApiKey(apiKey);record.setIssuedAt(now);record.setOwnerToken(token);record.setUpdatedAt(Instant.now());store.saveToken(record);return token;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public String getOwnerToken(String ownerId, String apiId, String apiKey) {
|
|
|
|
|
+ if (ownerId == null || ownerId.isBlank()
|
|
|
|
|
+ || apiId == null || apiId.isBlank()
|
|
|
|
|
+ || apiKey == null || apiKey.isBlank()) {
|
|
|
|
|
+ throw new IllegalStateException("OPPO ownerId/apiId/apiKey are required");
|
|
|
|
|
+ }
|
|
|
|
|
+ long now = Instant.now().getEpochSecond();
|
|
|
|
|
+ String sign = sha1(apiId + apiKey + now);
|
|
|
|
|
+ return Base64.getEncoder().encodeToString(
|
|
|
|
|
+ (ownerId + "," + apiId + "," + now + "," + sign).getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static String sha1(String value) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ byte[] digest = MessageDigest.getInstance("SHA-1").digest(value.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
|
|
+ for (byte b : digest) result.append(String.format("%02x", b));
|
|
|
|
|
+ return result.toString();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new IllegalStateException("OPPO token sign failed", e);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- private static String sha1(String value){try{byte[] d=MessageDigest.getInstance("SHA-1").digest(value.getBytes(StandardCharsets.UTF_8));StringBuilder s=new StringBuilder();for(byte b:d)s.append(String.format("%02x",b));return s.toString();}catch(Exception e){throw new IllegalStateException("OPPO token sign failed",e);}}
|
|
|
|
|
}
|
|
}
|