|
| 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 | +} |
0 commit comments