Skip to content

Commit 905b644

Browse files
committed
声网token生成
1 parent 17d11f9 commit 905b644

20 files changed

+1102
-1
lines changed

.idea/workspace.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package io.agora.media;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.util.TreeMap;
6+
7+
import static io.agora.media.Utils.crc32;
8+
9+
public class AccessToken {
10+
public enum Privileges {
11+
kJoinChannel(1),
12+
kPublishAudioStream(2),
13+
kPublishVideoStream(3),
14+
kPublishDataStream(4),
15+
16+
// For RTM only
17+
kRtmLogin(1000);
18+
19+
// The following privileges have not
20+
// been implemented yet.
21+
22+
//kPublishAudiocdn(5),
23+
//kPublishVideoCdn(6),
24+
//kRequestPublishAudioStream(7),
25+
//kRequestPublishVideoStream(8),
26+
//kRequestPublishDataStream(9),
27+
//kInvitePublishAudioStream(10),
28+
//kInvitePublishVideoStream(11),
29+
//kInvitePublishDataStream(12),
30+
//kAdministrateChannel(101),
31+
32+
public short intValue;
33+
34+
Privileges(int value) {
35+
intValue = (short) value;
36+
}
37+
}
38+
39+
private static final String VER = "006";
40+
41+
public String appId;
42+
public String appCertificate;
43+
public String channelName;
44+
public String uid;
45+
public byte[] signature;
46+
public byte[] messageRawContent;
47+
public int crcChannelName;
48+
public int crcUid;
49+
public PrivilegeMessage message;
50+
public int expireTimestamp;
51+
52+
public AccessToken(String appId, String appCertificate, String channelName, String uid) {
53+
this.appId = appId;
54+
this.appCertificate = appCertificate;
55+
this.channelName = channelName;
56+
this.uid = uid;
57+
this.crcChannelName = 0;
58+
this.crcUid = 0;
59+
this.message = new PrivilegeMessage();
60+
}
61+
62+
public String build() throws Exception {
63+
if (! Utils.isUUID(appId)) {
64+
return "";
65+
}
66+
67+
if (!Utils.isUUID(appCertificate)) {
68+
return "";
69+
}
70+
71+
messageRawContent = Utils.pack(message);
72+
signature = generateSignature(appCertificate,
73+
appId, channelName, uid, messageRawContent);
74+
crcChannelName = crc32(channelName);
75+
crcUid = crc32(uid);
76+
77+
PackContent packContent = new PackContent(signature, crcChannelName, crcUid, messageRawContent);
78+
byte[] content = Utils.pack(packContent);
79+
return getVersion() + this.appId + Utils.base64Encode(content);
80+
}
81+
82+
public void addPrivilege(Privileges privilege, int expireTimestamp) {
83+
message.messages.put(privilege.intValue, expireTimestamp);
84+
}
85+
86+
public static String getVersion() {
87+
return VER;
88+
}
89+
90+
public static byte[] generateSignature(String appCertificate,
91+
String appID, String channelName, String uid, byte[] message) throws Exception {
92+
93+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
94+
try {
95+
baos.write(appID.getBytes());
96+
baos.write(channelName.getBytes());
97+
baos.write(uid.getBytes());
98+
baos.write(message);
99+
} catch (IOException e) {
100+
e.printStackTrace();
101+
}
102+
return Utils.hmacSign(appCertificate, baos.toByteArray());
103+
}
104+
105+
public boolean fromString(String token) {
106+
if (!getVersion().equals(token.substring(0, Utils.VERSION_LENGTH))) {
107+
return false;
108+
}
109+
110+
try {
111+
appId = token.substring(Utils.VERSION_LENGTH, Utils.VERSION_LENGTH + Utils.APP_ID_LENGTH);
112+
PackContent packContent = new PackContent();
113+
Utils.unpack(Utils.base64Decode(token.substring(Utils.VERSION_LENGTH + Utils.APP_ID_LENGTH, token.length())), packContent);
114+
signature = packContent.signature;
115+
crcChannelName = packContent.crcChannelName;
116+
crcUid = packContent.crcUid;
117+
messageRawContent = packContent.rawMessage;
118+
Utils.unpack(messageRawContent, message);
119+
} catch (Exception e) {
120+
e.printStackTrace();
121+
return false;
122+
}
123+
124+
return true;
125+
}
126+
127+
public class PrivilegeMessage implements PackableEx {
128+
public int salt;
129+
public int ts;
130+
public TreeMap<Short, Integer> messages;
131+
132+
public PrivilegeMessage() {
133+
salt = Utils.randomInt();
134+
ts = Utils.getTimestamp() + 24 * 3600;
135+
messages = new TreeMap<>();
136+
}
137+
138+
@Override
139+
public ByteBuf marshal(ByteBuf out) {
140+
return out.put(salt).put(ts).putIntMap(messages);
141+
}
142+
143+
@Override
144+
public void unmarshal(ByteBuf in) {
145+
salt = in.readInt();
146+
ts = in.readInt();
147+
messages = in.readIntMap();
148+
}
149+
}
150+
151+
public class PackContent implements PackableEx {
152+
public byte[] signature;
153+
public int crcChannelName;
154+
public int crcUid;
155+
public byte[] rawMessage;
156+
157+
public PackContent() {
158+
// Nothing done
159+
}
160+
161+
public PackContent(byte[] signature, int crcChannelName, int crcUid, byte[] rawMessage) {
162+
this.signature = signature;
163+
this.crcChannelName = crcChannelName;
164+
this.crcUid = crcUid;
165+
this.rawMessage = rawMessage;
166+
}
167+
168+
@Override
169+
public ByteBuf marshal(ByteBuf out) {
170+
return out.put(signature).put(crcChannelName).put(crcUid).put(rawMessage);
171+
}
172+
173+
@Override
174+
public void unmarshal(ByteBuf in) {
175+
signature = in.readBytes();
176+
crcChannelName = in.readInt();
177+
crcUid = in.readInt();
178+
rawMessage = in.readBytes();
179+
}
180+
}
181+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package io.agora.media;
2+
3+
import java.nio.ByteBuffer;
4+
import java.nio.ByteOrder;
5+
import java.util.Map;
6+
import java.util.TreeMap;
7+
8+
/**
9+
* Created by Li on 10/1/2016.
10+
*/
11+
public class ByteBuf {
12+
ByteBuffer buffer = ByteBuffer.allocate(1024).order(ByteOrder.LITTLE_ENDIAN);
13+
14+
public ByteBuf() {
15+
}
16+
17+
public ByteBuf(byte[] bytes) {
18+
this.buffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
19+
}
20+
21+
public byte[] asBytes() {
22+
byte[] out = new byte[buffer.position()];
23+
buffer.rewind();
24+
buffer.get(out, 0, out.length);
25+
return out;
26+
}
27+
28+
// packUint16
29+
public ByteBuf put(short v) {
30+
buffer.putShort(v);
31+
return this;
32+
}
33+
34+
public ByteBuf put(byte[] v) {
35+
put((short)v.length);
36+
buffer.put(v);
37+
return this;
38+
}
39+
40+
// packUint32
41+
public ByteBuf put(int v) {
42+
buffer.putInt(v);
43+
return this;
44+
}
45+
46+
public ByteBuf put(long v) {
47+
buffer.putLong(v);
48+
return this;
49+
}
50+
51+
public ByteBuf put(String v) {
52+
return put(v.getBytes());
53+
}
54+
55+
public ByteBuf put(TreeMap<Short, String> extra) {
56+
put((short)extra.size());
57+
58+
for (Map.Entry<Short, String> pair : extra.entrySet()) {
59+
put(pair.getKey());
60+
put(pair.getValue());
61+
}
62+
63+
return this;
64+
}
65+
66+
public ByteBuf putIntMap(TreeMap<Short, Integer> extra) {
67+
put((short)extra.size());
68+
69+
for (Map.Entry<Short, Integer> pair : extra.entrySet()) {
70+
put(pair.getKey());
71+
put(pair.getValue());
72+
}
73+
74+
return this;
75+
}
76+
77+
public short readShort() {
78+
return buffer.getShort();
79+
}
80+
81+
82+
public int readInt() {
83+
return buffer.getInt();
84+
}
85+
86+
public byte[] readBytes() {
87+
short length = readShort();
88+
byte[] bytes = new byte[length];
89+
buffer.get(bytes);
90+
return bytes;
91+
}
92+
93+
public String readString() {
94+
byte[] bytes = readBytes();
95+
return new String(bytes);
96+
}
97+
98+
public TreeMap readMap() {
99+
TreeMap<Short, String> map = new TreeMap<>();
100+
101+
short length = readShort();
102+
103+
for (short i = 0; i < length; ++i) {
104+
short k = readShort();
105+
String v = readString();
106+
map.put(k, v);
107+
}
108+
109+
return map;
110+
}
111+
112+
public TreeMap<Short, Integer> readIntMap() {
113+
TreeMap<Short, Integer> map = new TreeMap<>();
114+
115+
short length = readShort();
116+
117+
for (short i = 0; i < length; ++i) {
118+
short k = readShort();
119+
Integer v = readInt();
120+
map.put(k, v);
121+
}
122+
123+
return map;
124+
}
125+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.agora.media;
2+
3+
import java.io.ByteArrayOutputStream;
4+
5+
/**
6+
* Created by hefeng on 15/8/10.
7+
* Util to generate Agora media dynamic key.
8+
*/
9+
public class DynamicKey {
10+
/**
11+
* Generate Dynamic Key for media channel service
12+
* @param appID App ID assigned by Agora
13+
* @param appCertificate App Certificate assigned by Agora
14+
* @param channelName name of channel to join, limited to 64 bytes and should be printable ASCII characters
15+
* @param unixTs unix timestamp in seconds when generating the Dynamic Key
16+
* @param randomInt salt for generating dynamic key
17+
* @return String representation of dynamic key
18+
* @throws Exception
19+
*/
20+
public static String generate(String appID, String appCertificate, String channelName, int unixTs, int randomInt) throws Exception {
21+
String unixTsStr = ("0000000000" + Integer.toString(unixTs)).substring(Integer.toString(unixTs).length());
22+
String randomIntStr = ("00000000" + Integer.toHexString(randomInt)).substring(Integer.toHexString(randomInt).length());
23+
String signature = generateSignature(appID, appCertificate, channelName, unixTsStr, randomIntStr);
24+
return String.format("%s%s%s%s", signature, appID, unixTsStr, randomIntStr);
25+
}
26+
27+
private static String generateSignature(String appID, String appCertificate, String channelName, String unixTsStr, String randomIntStr) throws Exception {
28+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
29+
baos.write(appID.getBytes());
30+
baos.write(unixTsStr.getBytes());
31+
baos.write(randomIntStr.getBytes());
32+
baos.write(channelName.getBytes());
33+
byte[] sign = DynamicKeyUtil.encodeHMAC(appCertificate, baos.toByteArray());
34+
return DynamicKeyUtil.bytesToHex(sign);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)