|
| 1 | +# https://github.com/8x8/jaas_demo/blob/main/jaas-jwt-samples/python/jass-jwt.py |
| 2 | + |
| 3 | +import sys |
| 4 | +import os |
| 5 | +import time |
| 6 | +import uuid |
| 7 | +from authlib.jose import jwt |
| 8 | + |
| 9 | + |
| 10 | +class JaaSJwtBuilder: |
| 11 | + """ |
| 12 | + The JaaSJwtBuilder class helps with the generation of the JaaS JWT. |
| 13 | + """ |
| 14 | + |
| 15 | + EXP_TIME_DELAY_SEC = 7200 |
| 16 | + # Used as a delay for the exp claim value. |
| 17 | + |
| 18 | + NBF_TIME_DELAY_SEC = 10 |
| 19 | + # Used as a delay for the nbf claim value. |
| 20 | + |
| 21 | + def __init__(self) -> None: |
| 22 | + self.header = {'alg': 'RS256'} |
| 23 | + self.userClaims = {} |
| 24 | + self.featureClaims = {} |
| 25 | + self.payloadClaims = {} |
| 26 | + |
| 27 | + def withDefaults(self): |
| 28 | + """Returns the JaaSJwtBuilder with default valued claims.""" |
| 29 | + return self.withExpTime(int(time.time() + JaaSJwtBuilder.EXP_TIME_DELAY_SEC)) \ |
| 30 | + .withNbfTime(int(time.time() - JaaSJwtBuilder.NBF_TIME_DELAY_SEC)) \ |
| 31 | + .withLiveStreamingEnabled(False) \ |
| 32 | + .withRecordingEnabled(False) \ |
| 33 | + .withOutboundCallEnabled(False) \ |
| 34 | + .withTranscriptionEnabled(False) \ |
| 35 | + .withModerator(True) \ |
| 36 | + .withRoomName('*') \ |
| 37 | + .withUserId(str(uuid.uuid4())) |
| 38 | + |
| 39 | + def withApiKey(self, apiKey): |
| 40 | + """ |
| 41 | + Returns the JaaSJwtBuilder with the kid claim(apiKey) set. |
| 42 | +
|
| 43 | + :param apiKey A string as the API Key https://jaas.8x8.vc/#/apikeys |
| 44 | + """ |
| 45 | + self.header['kid'] = apiKey |
| 46 | + return self |
| 47 | + |
| 48 | + def withUserAvatar(self, avatarUrl): |
| 49 | + """ |
| 50 | + Returns the JaaSJwtBuilder with the avatar claim set. |
| 51 | +
|
| 52 | + :param avatarUrl A string representing the url to get the user avatar. |
| 53 | + """ |
| 54 | + self.userClaims['avatar'] = avatarUrl |
| 55 | + return self |
| 56 | + |
| 57 | + def withModerator(self, isModerator): |
| 58 | + """ |
| 59 | + Returns the JaaSJwtBuilder with the moderator claim set. |
| 60 | +
|
| 61 | + :param isModerator A boolean if set to True, user is moderator and False otherwise. |
| 62 | + """ |
| 63 | + self.userClaims['moderator'] = 'true' if isModerator == True else 'false' |
| 64 | + return self |
| 65 | + |
| 66 | + def withUserName(self, userName): |
| 67 | + """ |
| 68 | + Returns the JaaSJwtBuilder with the name claim set. |
| 69 | +
|
| 70 | + :param userName A string representing the user's name. |
| 71 | + """ |
| 72 | + self.userClaims['name'] = userName |
| 73 | + return self |
| 74 | + |
| 75 | + def withUserEmail(self, userEmail): |
| 76 | + """ |
| 77 | + Returns the JaaSJwtBuilder with the email claim set. |
| 78 | +
|
| 79 | + :param userEmail A string representing the user's email address. |
| 80 | + """ |
| 81 | + self.userClaims['email'] = userEmail |
| 82 | + return self |
| 83 | + |
| 84 | + def withLiveStreamingEnabled(self, isEnabled): |
| 85 | + """ |
| 86 | + Returns the JaaSJwtBuilder with the livestreaming claim set. |
| 87 | +
|
| 88 | + :param isEnabled A boolean if set to True, live streaming is enabled and False otherwise. |
| 89 | + """ |
| 90 | + self.featureClaims['livestreaming'] = 'true' if isEnabled == True else 'false' |
| 91 | + return self |
| 92 | + |
| 93 | + def withRecordingEnabled(self, isEnabled): |
| 94 | + """ |
| 95 | + Returns the JaaSJwtBuilder with the recording claim set. |
| 96 | +
|
| 97 | + :param isEnabled A boolean if set to True, recording is enabled and False otherwise. |
| 98 | + """ |
| 99 | + self.featureClaims['recording'] = 'true' if isEnabled == True else 'false' |
| 100 | + return self |
| 101 | + |
| 102 | + def withTranscriptionEnabled(self, isEnabled): |
| 103 | + """ |
| 104 | + Returns the JaaSJwtBuilder with the transcription claim set. |
| 105 | +
|
| 106 | + :param isEnabled A boolean if set to True, transcription is enabled and False otherwise. |
| 107 | + """ |
| 108 | + self.featureClaims['transcription'] = 'true' if isEnabled == True else 'false' |
| 109 | + return self |
| 110 | + |
| 111 | + def withOutboundCallEnabled(self, isEnabled): |
| 112 | + """ |
| 113 | + Returns the JaaSJwtBuilder with the outbound-call claim set. |
| 114 | +
|
| 115 | + :param isEnabled A boolean if set to True, outbound calls are enabled and False otherwise. |
| 116 | + """ |
| 117 | + self.featureClaims['outbound-call'] = 'true' if isEnabled == True else 'false' |
| 118 | + return self |
| 119 | + |
| 120 | + def withExpTime(self, expTime): |
| 121 | + """ |
| 122 | + Returns the JaaSJwtBuilder with exp claim set. Use the defaults, you won't have to change this value too much. |
| 123 | +
|
| 124 | + :param expTime Unix time in seconds since epochs plus a delay. Expiration time of the JWT. |
| 125 | + """ |
| 126 | + self.payloadClaims['exp'] = expTime |
| 127 | + return self |
| 128 | + |
| 129 | + def withNbfTime(self, nbfTime): |
| 130 | + """ |
| 131 | + Returns the JaaSJwtBuilder with nbf claim set. Use the defaults, you won't have to change this value too much. |
| 132 | +
|
| 133 | + :param nbfTime Unix time in seconds since epochs. |
| 134 | + """ |
| 135 | + self.payloadClaims['nbfTime'] = nbfTime |
| 136 | + return self |
| 137 | + |
| 138 | + def withRoomName(self, roomName): |
| 139 | + """ |
| 140 | + Returns the JaaSJwtBuilder with room claim set. |
| 141 | +
|
| 142 | + :param roomName A string representing the room to join. |
| 143 | + """ |
| 144 | + self.payloadClaims['room'] = roomName |
| 145 | + return self |
| 146 | + |
| 147 | + def withAppID(self, AppId): |
| 148 | + """ |
| 149 | + Returns the JaaSJwtBuilder with the sub claim set. |
| 150 | +
|
| 151 | + :param AppId A string representing the unique AppID (previously tenant). |
| 152 | + """ |
| 153 | + self.payloadClaims['sub'] = AppId |
| 154 | + return self |
| 155 | + |
| 156 | + def withUserId(self, userId): |
| 157 | + """ |
| 158 | + Returns the JaaSJwtBuilder with the id claim set. |
| 159 | +
|
| 160 | + :param A string representing the user, should be unique from your side. |
| 161 | + """ |
| 162 | + self.userClaims['id'] = userId |
| 163 | + return self |
| 164 | + |
| 165 | + def signWith(self, key): |
| 166 | + """ |
| 167 | + Returns a signed JWT. |
| 168 | +
|
| 169 | + :param key A string representing the private key in PEM format. |
| 170 | + """ |
| 171 | + context = {'user': self.userClaims, 'features': self.featureClaims} |
| 172 | + self.payloadClaims['context'] = context |
| 173 | + self.payloadClaims['iss'] = 'chat' |
| 174 | + self.payloadClaims['aud'] = 'jitsi' |
| 175 | + return jwt.encode(self.header, self.payloadClaims, key) |
0 commit comments