All files / src/composables useKeycloak.ts

0% Statements 0/148
0% Branches 0/1
0% Functions 0/1
0% Lines 0/148

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
import { UserManager, WebStorageStateStore, InMemoryWebStorage, User } from 'oidc-client-ts'
import { ref, readonly } from 'vue'
import { env } from '../env'
 
// ----------------------------------------------------------------
// KEYCLOAK CONFIGURATION
// ----------------------------------------------------------------
const KEYCLOAK_URL = env.KEYCLOAK_URL
const KEYCLOAK_REALM = env.KEYCLOAK_REALM
const CLIENT_ID = env.KEYCLOAK_CLIENT_ID
const APP_URL = env.APP_URL
 
// ----------------------------------------------------------------
// USER MANAGER SETUP
// ----------------------------------------------------------------
// Tokens live in memory only — never in localStorage. Any XSS payload that
// runs in the page would otherwise be able to exfiltrate a long-lived access
// token. The trade-off is that a hard reload drops the in-tab session; we
// rely on Keycloak's SSO cookie + signinSilent to re-issue tokens on demand.
const userManager = new UserManager({
  authority: `${KEYCLOAK_URL}/realms/${KEYCLOAK_REALM}`,
  client_id: CLIENT_ID,
  redirect_uri: `${APP_URL}/callback`,
  post_logout_redirect_uri: `${APP_URL}/login`,
  response_type: 'code',
  scope: 'openid profile email',
 
  userStore: new WebStorageStateStore({ store: new InMemoryWebStorage() }),
 
  // Automatic silent renew
  automaticSilentRenew: true,
  silent_redirect_uri: `${APP_URL}/silent-refresh.html`,
 
  // Token settings
  loadUserInfo: true,
 
  // Metadata settings (optional, will be fetched automatically)
  metadataUrl: `${KEYCLOAK_URL}/realms/${KEYCLOAK_REALM}/.well-known/openid-configuration`,
})
 
// ----------------------------------------------------------------
// REACTIVE STATE
// ----------------------------------------------------------------
const user = ref<User | null>(null)
const isLoading = ref(true)
const isAuthenticated = ref(false)
 
// ----------------------------------------------------------------
// REFRESH SINGLE-FLIGHT
// ----------------------------------------------------------------
// Multiple parallel requests routinely hit the 401-then-refresh path at the
// same time (page load fires 5+ XHRs). Without coalescing, each one calls
// signinSilent() independently, which spawns 5+ hidden iframes and races —
// some succeed, some fail with `login_required`, and the user is bounced
// back to the login page even though Keycloak's SSO cookie is fine.
//
// Module-scoped because the UserManager is module-scoped: every consumer of
// `useKeycloak()` shares the same in-flight refresh.
let refreshInFlight: Promise<User | null> | null = null
 
async function refreshTokenSingleFlight(): Promise<User | null> {
  if (refreshInFlight) {
    return refreshInFlight
  }
  refreshInFlight = (async () => {
    try {
      const refreshed = await userManager.signinSilent()
      user.value = refreshed
      isAuthenticated.value = !!refreshed
      return refreshed
    } finally {
      refreshInFlight = null
    }
  })()
  return refreshInFlight
}
 
// ----------------------------------------------------------------
// COMPOSABLE
// ----------------------------------------------------------------
export function useKeycloak() {
  /**
   * Initialize authentication - load user from storage, falling back to a
   * silent SSO refresh against Keycloak when the in-memory store is empty
   * (e.g. after a hard reload). Without this fallback, every full page reload
   * forces an interactive login redirect even though Keycloak's session
   * cookie is still valid.
   */
  async function initialize() {
    isLoading.value = true
    try {
      const storedUser = await userManager.getUser()
      if (storedUser && !storedUser.expired) {
        user.value = storedUser
        isAuthenticated.value = true
        return
      }
 
      try {
        const refreshed = await refreshTokenSingleFlight()
        if (refreshed && !refreshed.expired) {
          user.value = refreshed
          isAuthenticated.value = true
          return
        }
      } catch {
        // No SSO session available — fall through to unauthenticated state.
      }
 
      user.value = null
      isAuthenticated.value = false
    } catch (error) {
      console.error('Failed to initialize auth:', error)
      user.value = null
      isAuthenticated.value = false
    } finally {
      isLoading.value = false
    }
  }
 
  /**
   * Redirect to Keycloak login
   */
  async function login(returnUrl?: string) {
    try {
      // Store return URL for after login
      if (returnUrl) {
        sessionStorage.setItem('returnUrl', returnUrl)
      }
      await userManager.signinRedirect()
    } catch (error) {
      console.error('Login failed:', error)
      throw error
    }
  }
 
  /**
   * Handle callback from Keycloak after login
   */
  async function handleCallback() {
    try {
      const callbackUser = await userManager.signinRedirectCallback()
      user.value = callbackUser
      isAuthenticated.value = true
 
      // Get return URL and clean up
      const returnUrl = sessionStorage.getItem('returnUrl') || '/'
      sessionStorage.removeItem('returnUrl')
 
      return returnUrl
    } catch (error) {
      console.error('Callback handling failed:', error)
      throw error
    }
  }
 
  /**
   * Logout and redirect to Keycloak
   */
  async function logout() {
    try {
      await userManager.signoutRedirect()
      user.value = null
      isAuthenticated.value = false
    } catch (error) {
      console.error('Logout failed:', error)
      throw error
    }
  }
 
  /**
   * Get current access token, refreshing it once via the shared single-flight
   * promise if it has expired. Returns null if no session is present or the
   * refresh fails.
   */
  async function getAccessToken(): Promise<string | null> {
    try {
      const currentUser = await userManager.getUser()
      if (!currentUser) return null
      if (!currentUser.expired) return currentUser.access_token || null
 
      const refreshed = await refreshTokenSingleFlight()
      return refreshed?.access_token || null
    } catch (error) {
      console.error('Failed to get access token:', error)
      return null
    }
  }
 
  /**
   * Get user info
   */
  async function getUser(): Promise<User | null> {
    try {
      return await userManager.getUser()
    } catch (error) {
      console.error('Failed to get user:', error)
      return null
    }
  }
 
  /**
   * Check if token is expired and refresh if needed.
   * All concurrent callers share a single signinSilent() round-trip.
   */
  async function ensureValidToken(): Promise<boolean> {
    try {
      const currentUser = await userManager.getUser()
      if (!currentUser) return false
      if (!currentUser.expired) return true
 
      const refreshed = await refreshTokenSingleFlight()
      return !!refreshed && !refreshed.expired
    } catch (error) {
      console.error('Token refresh failed:', error)
      return false
    }
  }
 
  return {
    // State
    user: readonly(user),
    isLoading: readonly(isLoading),
    isAuthenticated: readonly(isAuthenticated),
 
    // Methods
    initialize,
    login,
    handleCallback,
    logout,
    getAccessToken,
    getUser,
    ensureValidToken,
  }
}