Greetings! We are recently trying to implement AppToken authorisation for our NextJS application . Following the workflow, if I copy and paste the step 3 setup code, I can get a successful privileged session creation. However, there our admin secret is used to create the ‘unprivileged’ session to fetch the appToken.
Trying to create the unprivileged session via startWidgetSession and using that session to compute the tokenHash as described in preceding steps to fetch the token I get errors of invalid app token hash.
The app token was created for us by an account administrator as type USER with Sha256.
Below the 3 approaches tried - both the first approaches produce the same result of invalid hash. The last is the approach copy/pasted from the workflow which works but needs the admin secret. Can you advise is there something wrong with the way we create the hash or other steps in NodeJS?
Thank you!
const crypto = require('crypto')
const kaltura = require('kaltura-client')
export async function getAppTokenSession(): Promise<any> {
const config = new kaltura.Configuration()
config.serviceUrl = 'https://www.kaltura.com'
const client = new kaltura.Client(config)
const widgetId = `_${process.env.NEXT_PUBLIC_KALTURA_PARTNER_ID}`
const expiry = 86400
// console.log('Entered getAppTokenSession()')
// kaltura.services.session
// .startWidgetSession(widgetId, expiry)
// .execute(client)
// .then((success) => {
// console.log('success', success)
// client.setKs(success.ks)
// //const shasum = crypto.createHash('sha256')
// //shasum.update(client.ks + process.env.KALTURA_APP_TOKEN)
// // const hash = shasum.digest('hex')
// const hash = crypto
// .createHash('sha256')
// .update(client.ks + process.env.KALTURA_APP_TOKEN)
// .digest('hex')
// console.log('**HASH**', hash)
// const id = process.env.KALTURA_APP_TOKEN_ID
// const tokenHash = hash
// const userId = process.env.KALTURA_USER
// const type = kaltura.enums.SessionType.USER
// const appTokenSessionExpiry = 0
// const sessionPrivileges = ''
// kaltura.services.appToken
// .startSession(
// id,
// tokenHash,
// userId,
// type,
// appTokenSessionExpiry,
// sessionPrivileges
// )
// .execute(client)
// .then((result) => {
// console.log('got app token without C&P Step 3 workflow')
// console.log(result)
// })
// })
const widgetSession = await kaltura.services.session
.startWidgetSession(widgetId, expiry)
.execute(client)
console.log('widgetSession', widgetSession)
const shasum = crypto.createHash('sha256')
client.setKs(widgetSession.ks)
shasum.update(client.ks + process.env.KALTURA_APP_TOKEN)
const hash = shasum.digest('hex')
console.log('**HASH**', hash)
const id = process.env.KALTURA_APP_TOKEN_ID
const tokenHash = hash
const userId = process.env.KALTURA_USER
const type = kaltura.enums.SessionType.USER
const appTokenSessionExpiry = 0
const sessionPrivileges = ''
kaltura.services.appToken
.startSession(
id,
tokenHash,
userId,
type,
appTokenSessionExpiry,
sessionPrivileges
)
.execute(client)
.then((result) => {
console.log('got app token without C&P Step 3 workflow')
console.log(result)
})
// THIS WORKS BUT USES SECRET TO START UNPRIVILEGED SESSION
// kaltura.services.session
// .start(
// process.env.KALTURA_ADMIN_SECRET,
// process.env.KALTURA_USER,
// kaltura.enums.SessionType.ADMIN,
// 1936261
// )
// .completion((success, ks) => {
// if (!success) {
// throw new Error(ks.message)
// }
// const shasum = crypto.createHash('sha256')
// client.ks = ks
// shasum.update(client.ks + process.env.KALTURA_APP_TOKEN)
// const hash = shasum.digest('hex')
// client.setKs(ks)
// const id = process.env.KALTURA_APP_TOKEN_ID
// const tokenHash = hash
// const userId = process.env.KALTURA_USER
// const type = kaltura.enums.SessionType.ADMIN
// const expiry = 0
// const sessionPrivileges = ''
// kaltura.services.appToken
// .startSession(id, tokenHash, userId, type, expiry, sessionPrivileges)
// .execute(client)
// .then((result) => {
// console.log('got app token woo!')
// console.log(result)
// })
// })
// .execute(client)
}