|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2026 LiveKit, Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# Generate a LiveKit access token via `lk` and set LIVEKIT_TOKEN (and LIVEKIT_URL) |
| 17 | +# for your current shell session. |
| 18 | +# |
| 19 | +# source examples/tokens/gen_and_set.bash --id PARTICIPANT_ID --room ROOM_NAME [--view-token] |
| 20 | +# eval "$(bash examples/tokens/gen_and_set.bash --id ID --room ROOM [--view-token])" |
| 21 | +# |
| 22 | +# Optional env: LIVEKIT_API_KEY, LIVEKIT_API_SECRET, LIVEKIT_VALID_FOR. |
| 23 | + |
| 24 | +# When sourced, we must NOT enable errexit/pipefail on the interactive shell — a |
| 25 | +# failing pipeline (e.g. sed|head SIGPIPE) or any error would close your terminal. |
| 26 | + |
| 27 | +_sourced=0 |
| 28 | +if [[ -n "${BASH_VERSION:-}" ]] && [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then |
| 29 | + _sourced=1 |
| 30 | +elif [[ -n "${ZSH_VERSION:-}" ]] && [[ "${ZSH_EVAL_CONTEXT:-}" == *:file* ]]; then |
| 31 | + _sourced=1 |
| 32 | +fi |
| 33 | + |
| 34 | +_fail() { |
| 35 | + echo "gen_and_set.bash: $1" >&2 |
| 36 | + if [[ "$_sourced" -eq 1 ]]; then |
| 37 | + return "${2:-1}" |
| 38 | + fi |
| 39 | + exit "${2:-1}" |
| 40 | +} |
| 41 | + |
| 42 | +_usage() { |
| 43 | + echo "Usage: ${0##*/} --id PARTICIPANT_IDENTITY --room ROOM_NAME [--view-token]" >&2 |
| 44 | + echo " --id LiveKit participant identity (required)" >&2 |
| 45 | + echo " --room Room name (required; not read from env)" >&2 |
| 46 | + echo " --view-token Print the JWT to stderr after generating" >&2 |
| 47 | +} |
| 48 | + |
| 49 | +if [[ "$_sourced" -eq 0 ]]; then |
| 50 | + set -euo pipefail |
| 51 | +fi |
| 52 | + |
| 53 | +_view_token=0 |
| 54 | +LIVEKIT_IDENTITY="" |
| 55 | +LIVEKIT_ROOM="robo_room" |
| 56 | +while [[ $# -gt 0 ]]; do |
| 57 | + case "$1" in |
| 58 | + --view-token) |
| 59 | + _view_token=1 |
| 60 | + shift |
| 61 | + ;; |
| 62 | + --id) |
| 63 | + if [[ $# -lt 2 ]]; then |
| 64 | + _usage |
| 65 | + _fail "--id requires a value" 2 |
| 66 | + fi |
| 67 | + LIVEKIT_IDENTITY="$2" |
| 68 | + shift 2 |
| 69 | + ;; |
| 70 | + --room) |
| 71 | + if [[ $# -lt 2 ]]; then |
| 72 | + _usage |
| 73 | + _fail "--room requires a value" 2 |
| 74 | + fi |
| 75 | + LIVEKIT_ROOM="$2" |
| 76 | + shift 2 |
| 77 | + ;; |
| 78 | + -h | --help) |
| 79 | + _usage |
| 80 | + if [[ "$_sourced" -eq 1 ]]; then |
| 81 | + return 0 |
| 82 | + fi |
| 83 | + exit 0 |
| 84 | + ;; |
| 85 | + *) |
| 86 | + _usage |
| 87 | + _fail "unknown argument: $1" 2 |
| 88 | + ;; |
| 89 | + esac |
| 90 | +done |
| 91 | + |
| 92 | +if [[ -z "$LIVEKIT_IDENTITY" ]]; then |
| 93 | + _usage |
| 94 | + _fail "--id is required" 2 |
| 95 | +fi |
| 96 | +if [[ -z "$LIVEKIT_ROOM" ]]; then |
| 97 | + _usage |
| 98 | + _fail "--room is required" 2 |
| 99 | +fi |
| 100 | + |
| 101 | +LIVEKIT_API_KEY="${LIVEKIT_API_KEY:-devkey}" |
| 102 | +LIVEKIT_API_SECRET="${LIVEKIT_API_SECRET:-secret}" |
| 103 | +LIVEKIT_VALID_FOR="${LIVEKIT_VALID_FOR:-99999h}" |
| 104 | +_grant_json='{"canPublish":true,"canSubscribe":true,"canPublishData":true}' |
| 105 | + |
| 106 | +if ! command -v lk >/dev/null 2>&1; then |
| 107 | + _fail "'lk' CLI not found. Install: https://docs.livekit.io/home/cli/" 2 |
| 108 | +fi |
| 109 | + |
| 110 | +# Run lk inside bash so --grant JSON (with embedded ") is safe when this file is |
| 111 | +# sourced from zsh; zsh misparses --grant "$json" on the same line. |
| 112 | +_out="$( |
| 113 | + bash -c ' |
| 114 | + lk token create \ |
| 115 | + --api-key "$1" \ |
| 116 | + --api-secret "$2" \ |
| 117 | + -i "$3" \ |
| 118 | + --join \ |
| 119 | + --valid-for "$4" \ |
| 120 | + --room "$5" \ |
| 121 | + --grant "$6" 2>&1 |
| 122 | + ' _ "$LIVEKIT_API_KEY" "$LIVEKIT_API_SECRET" "$LIVEKIT_IDENTITY" \ |
| 123 | + "$LIVEKIT_VALID_FOR" "$LIVEKIT_ROOM" "$_grant_json" |
| 124 | +)" |
| 125 | +_lk_st=$? |
| 126 | +if [[ "$_lk_st" -ne 0 ]]; then |
| 127 | + echo "$_out" >&2 |
| 128 | + _fail "lk token create failed" 1 |
| 129 | +fi |
| 130 | + |
| 131 | +# Avoid sed|head pipelines (pipefail + SIGPIPE can kill a sourced shell). |
| 132 | +LIVEKIT_TOKEN="" |
| 133 | +LIVEKIT_URL="" |
| 134 | +while IFS= read -r _line || [[ -n "${_line}" ]]; do |
| 135 | + if [[ "$_line" == "Access token: "* ]]; then |
| 136 | + LIVEKIT_TOKEN="${_line#Access token: }" |
| 137 | + elif [[ "$_line" == "Project URL: "* ]]; then |
| 138 | + LIVEKIT_URL="${_line#Project URL: }" |
| 139 | + fi |
| 140 | +done <<< "$_out" |
| 141 | + |
| 142 | +if [[ -z "$LIVEKIT_TOKEN" ]]; then |
| 143 | + echo "gen_and_set.bash: could not parse Access token from lk output:" >&2 |
| 144 | + echo "$_out" >&2 |
| 145 | + _fail "missing Access token line" 1 |
| 146 | +fi |
| 147 | + |
| 148 | +if [[ "$_view_token" -eq 1 ]]; then |
| 149 | + echo "$LIVEKIT_TOKEN" >&2 |
| 150 | +fi |
| 151 | + |
| 152 | +_apply() { |
| 153 | + export LIVEKIT_TOKEN |
| 154 | + export LIVEKIT_URL |
| 155 | +} |
| 156 | + |
| 157 | +_emit_eval() { |
| 158 | + printf 'export LIVEKIT_TOKEN=%q\n' "$LIVEKIT_TOKEN" |
| 159 | + [[ -n "$LIVEKIT_URL" ]] && printf 'export LIVEKIT_URL=%q\n' "$LIVEKIT_URL" |
| 160 | +} |
| 161 | + |
| 162 | +if [[ "$_sourced" -eq 1 ]]; then |
| 163 | + _apply |
| 164 | + echo "LIVEKIT_TOKEN and LIVEKIT_URL set for this shell." >&2 |
| 165 | + [[ -n "$LIVEKIT_URL" ]] || echo "gen_and_set.bash: warning: no Project URL in output; set LIVEKIT_URL manually." >&2 |
| 166 | +else |
| 167 | + _emit_eval |
| 168 | + echo "gen_and_set.bash: for this shell run: source $0 --id ... --room ... or: eval \"\$(bash $0 ...)\"" >&2 |
| 169 | +fi |
0 commit comments