-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc2bed8
commit aa1822c
Showing
11 changed files
with
140 additions
and
48 deletions.
There are no files selected for viewing
Submodule docs
updated
from 97b8ef to 3cc94e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,4 @@ | |
|
||
.OmegaIdElement { | ||
max-width: 400px; | ||
> p { | ||
width: 100%; | ||
text-align: center; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,2 @@ | ||
import type { UserFiltered } from '@/services/users/Types' | ||
|
||
|
||
export const omegaIdFields = [ | ||
'id', | ||
'firstname', | ||
'lastname', | ||
'username', | ||
] as const satisfies (keyof UserFiltered)[] | ||
|
||
export const OmegaIdExpiryTime = 60 * 5 // 5 minutes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
import type { omegaIdFields } from './ConfigVars' | ||
import type { UserFiltered } from '@/services/users/Types' | ||
|
||
export type OmegaId = Pick<UserFiltered, typeof omegaIdFields[number]> | ||
export type OmegaId = Pick<UserFiltered, 'id'> | ||
|
||
export type OmegaIdJWT = { | ||
iat: number, | ||
exp: number, | ||
sub: UserFiltered['id'], | ||
usrnm: UserFiltered['username'], | ||
gn: UserFiltered['firstname'], | ||
sn: UserFiltered['lastname'], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { JWT_ISSUER } from '@/auth/ConfigVars' | ||
import type { ActionReturn, ActionReturnError } from '@/actions/Types' | ||
import type { OmegaIdJWT } from '@/services/omegaid/Types' | ||
|
||
/** | ||
* This file handles compression and decompression of omegaID | ||
*/ | ||
export function compressOmegaId(token: string): string { | ||
const parts = token.split('.') | ||
const payloadCompressed = compressPayload(parts[1]) | ||
const payload = base64ToBigInt(payloadCompressed) | ||
const sign = base64ToBigInt(parts[2]) | ||
const ret = `${payload}.${sign}` | ||
return ret | ||
} | ||
|
||
function decodeBase64Url(base64: string) { | ||
return atob(base64.replaceAll('-', '+').replaceAll('_', '/')) | ||
} | ||
|
||
function encodeBase64Url(data: string): string { | ||
return btoa(data).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '') | ||
} | ||
|
||
function base64ToBigInt(base64: string): string { | ||
const binaryString = decodeBase64Url(base64) | ||
let bigint = BigInt(0) | ||
|
||
// Convert binary string to BigInt | ||
for (let i = 0; i < binaryString.length; i++) { | ||
bigint = (bigint << BigInt(8)) | BigInt(binaryString.charCodeAt(i)) | ||
} | ||
|
||
return bigint.toString() | ||
} | ||
|
||
function bigIntToBase64(bigIntString: string): string { | ||
let bigInt = BigInt(bigIntString) | ||
let binaryString = '' | ||
|
||
while (bigInt > BigInt(0)) { | ||
const nextChar = bigInt & BigInt(0xFF) | ||
binaryString += String.fromCharCode(Number(nextChar)) | ||
bigInt >>= BigInt(8) | ||
} | ||
|
||
binaryString = binaryString.split('').reverse().join('') | ||
|
||
return encodeBase64Url(binaryString) | ||
} | ||
|
||
function compressPayload(payload: string): string { | ||
const binaryString = decodeBase64Url(payload) | ||
const payloadString = binaryString.toString() | ||
const payloadJSON = JSON.parse(payloadString) as OmegaIdJWT | ||
const shortPayloadString = `${payloadJSON.sub},${payloadJSON.iat},${payloadJSON.exp}` | ||
return encodeBase64Url(shortPayloadString) | ||
} | ||
|
||
function decompressPayload(rawdata: string): string { | ||
const base64String = bigIntToBase64(rawdata) | ||
const byteString = decodeBase64Url(base64String) | ||
const dataString = byteString.toString().split(',') | ||
const payload = { | ||
sub: Number(dataString[0]), | ||
iat: Number(dataString[1]), | ||
exp: Number(dataString[2]), | ||
aud: 'omegaid', | ||
iss: JWT_ISSUER, | ||
} | ||
const payloadString = JSON.stringify(payload) | ||
|
||
return encodeBase64Url(payloadString) | ||
} | ||
|
||
export function decomporessOmegaId(rawdata: string): ActionReturn<string> { | ||
const header = { | ||
alg: 'ES256', | ||
typ: 'JWT' | ||
} | ||
const headerJSONString = JSON.stringify(header) | ||
const headerB64String = encodeBase64Url(headerJSONString) | ||
|
||
const errorReturn: ActionReturnError = { | ||
success: false, | ||
errorCode: 'JWT INVALID', | ||
httpCode: 400, | ||
error: [{ | ||
message: 'QR code is not an OmegaId', | ||
}], | ||
} | ||
|
||
const rawDataSplit = rawdata.split('.') | ||
if (rawDataSplit.length !== 2) { | ||
return errorReturn | ||
} | ||
|
||
try { | ||
const payload = decompressPayload(rawDataSplit[0]) | ||
|
||
const signature = bigIntToBase64(rawDataSplit[1]) | ||
|
||
return { | ||
success: true, | ||
data: `${headerB64String}.${payload}.${signature}`, | ||
} | ||
} catch { | ||
return errorReturn | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters