bt-core-app 1.2.4 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,10 +31,17 @@ export interface CreateAuthOptions {
31
31
  expiryTokenFormat?: string;
32
32
  /**retrieve the auth item */
33
33
  getAuthItem: (navName?: string | AuthItem) => AuthItem | null;
34
- /**retrieve the url to navigate to for the OAuth 2.0 process */
35
- getAuthQuery: (redirectPath?: string, state?: string) => string;
36
- /**sets current credentials on top of default function*/
37
- setCredentials?: (state: RemovableRef<any>, payload: any) => void;
34
+ /**the url to start the OAuth 2.0 process */
35
+ getAuthorizeUrl?: (redirectPath?: string, state?: string) => string;
36
+ /**use the given code and generate the url to convert to an access token */
37
+ getTokenUrl?: (code: string, redirect_uri: string, grant_type: string, client_id: string) => string;
38
+ getToken?: () => Promise<void>;
39
+ oauthGrantType?: string;
40
+ oauthClientID?: string;
41
+ /**sets current credentials on top of default function
42
+ * for processing the token payload and applying to state
43
+ */
44
+ processTokenPayload?: (state: RemovableRef<any>, payload: any) => void;
38
45
  /**suboptions */
39
46
  subscriptionOptions?: AuthSubscription[];
40
47
  }
@@ -47,8 +54,9 @@ export interface BTAuth {
47
54
  credentials: any;
48
55
  doShow: (subcodes?: string[], permissions?: string[], action?: 'view' | 'edit') => boolean;
49
56
  doShowByNav: (navName?: string | AuthItem, includeChildren?: boolean) => boolean;
50
- getAuthUrl: (redirectPath?: string) => string;
57
+ getAuthorizeUrl: (redirectPath?: string) => string;
51
58
  getTimeZone: () => string;
59
+ getToken: () => Promise<void>;
52
60
  login: (redirectPath?: string) => void;
53
61
  logout: (navNameRedirect?: string) => void;
54
62
  setAuth: (jwtToken?: string) => void;
package/dist/core.d.ts CHANGED
@@ -1,19 +1,17 @@
1
- import { RemovableRef } from '@vueuse/core';
2
1
  import { CreateUrlOptions } from './composables/urls';
3
2
  import { NavigationItem } from './composables/navigation';
4
3
  import { BaseCosmeticTheme, UseCosmeticsOptions } from './composables/cosmetics';
5
- import { AuthSubscription } from './composables/auth';
4
+ import { CreateAuthOptions, AuthSubscription } from './composables/auth';
6
5
  import { App } from 'vue';
7
6
 
8
7
  export interface CoreApp {
9
8
  install(app: App): void;
10
9
  }
11
10
  export interface CreateCoreOptions extends UseCosmeticsOptions<BaseCosmeticTheme> {
11
+ auth: CreateAuthOptions;
12
12
  defaultCacheExpiryHours?: number;
13
- getAuthQuery: (redirectPath?: string, state?: string) => string;
14
13
  navItems?: NavigationItem[];
15
14
  presets: any;
16
- setCredentials?: (state: RemovableRef<any>, payload: any) => void;
17
15
  /**suboptions */
18
16
  subscriptionOptions?: AuthSubscription[];
19
17
  urls: CreateUrlOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bt-core-app",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "description": "Core app tools for some basic features like navigation, authentication, server apis, and cosmetics",
5
5
  "homepage": "https://github.com/BlitzItTech/bt-core",
6
6
  "bugs": {
@@ -1,8 +1,8 @@
1
- import { isLengthyArray } from '../composables/helpers'
1
+ import { appendUrl, isLengthyArray } from '../composables/helpers'
2
2
  import { DateTime } from 'luxon'
3
3
  import { type RemovableRef, useStorage } from '@vueuse/core'
4
4
  import { type BTDemo } from '../composables/demo'
5
- import { useRouter } from 'vue-router'
5
+ import { useRoute, useRouter } from 'vue-router'
6
6
  import { toValue } from 'vue'
7
7
  import { useAuthUrl } from './urls'
8
8
 
@@ -43,10 +43,17 @@ export interface CreateAuthOptions {
43
43
  expiryTokenFormat?: string
44
44
  /**retrieve the auth item */
45
45
  getAuthItem: (navName?: string | AuthItem) => AuthItem | null
46
- /**retrieve the url to navigate to for the OAuth 2.0 process */
47
- getAuthQuery: (redirectPath?: string, state?: string) => string
48
- /**sets current credentials on top of default function*/
49
- setCredentials?: (state: RemovableRef<any>, payload: any) => void
46
+ /**the url to start the OAuth 2.0 process */
47
+ getAuthorizeUrl?: (redirectPath?: string, state?: string) => string
48
+ /**use the given code and generate the url to convert to an access token */
49
+ getTokenUrl?: (code: string, redirect_uri: string, grant_type: string, client_id: string) => string
50
+ getToken?: () => Promise<void>
51
+ oauthGrantType?: string
52
+ oauthClientID?: string
53
+ /**sets current credentials on top of default function
54
+ * for processing the token payload and applying to state
55
+ */
56
+ processTokenPayload?: (state: RemovableRef<any>, payload: any) => void
50
57
  /**suboptions */
51
58
  subscriptionOptions?: AuthSubscription[]
52
59
  }
@@ -60,8 +67,9 @@ export interface BTAuth {
60
67
  credentials: any
61
68
  doShow: (subcodes?: string[], permissions?: string[], action?: 'view' | 'edit') => boolean
62
69
  doShowByNav: (navName?: string | AuthItem, includeChildren?: boolean) => boolean
63
- getAuthUrl: (redirectPath?: string) => string
70
+ getAuthorizeUrl: (redirectPath?: string) => string
64
71
  getTimeZone: () => string
72
+ getToken: () => Promise<void>
65
73
  login: (redirectPath?: string) => void
66
74
  logout: (navNameRedirect?: string) => void
67
75
  setAuth: (jwtToken?: string) => void
@@ -222,9 +230,11 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
222
230
  return doShowRes;
223
231
  }
224
232
 
225
- function getAuthUrl(redirectPath?: string) {
226
- return `${useAuthUrl()}?${options.getAuthQuery(redirectPath, authState.value)}`
227
- //return options.getAuthUrl(redirectPath, authState.value)
233
+ function getAuthorizeUrl(redirectPath?: string) {
234
+ if (options.getAuthorizeUrl != null)
235
+ return options.getAuthorizeUrl(redirectPath, authState.value) ?? ''
236
+
237
+ return ''
228
238
  }
229
239
 
230
240
  /**undefined or unfound is worth 0 */
@@ -289,11 +299,58 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
289
299
  options.demo?.endDemo()
290
300
  if (tokenExpired()) {
291
301
  logout()
292
- window.location.href = getAuthUrl(redirectPath) //}response_type=code&client_id=appClient1&redirect_path=${redirectPath}` : getAuthUrl()
302
+ window.location.href = getAuthorizeUrl(redirectPath) //}response_type=code&client_id=appClient1&redirect_path=${redirectPath}` : getAuthUrl()
293
303
  }
294
304
  }
295
305
  }
296
306
 
307
+ /**defaults to find parameters from route query */
308
+ async function getToken(code?: string, state?: string, redirect_path?: string) {
309
+ options.getToken ??= async () => {
310
+ const route = useRoute()
311
+
312
+ let mCode = code ?? route.query?.code as string
313
+ let mState = state ?? route.query?.state as string
314
+ let mRedirect = redirect_path ?? route.query?.redirect_path as string
315
+ let mGrantType = options.oauthGrantType ?? 'authorization_token'
316
+ let mClientID = options.oauthClientID ?? ''
317
+
318
+ if (mCode == null || mState == null) {
319
+ throw new Error('Code and State required in OAuth token process')
320
+ }
321
+
322
+ if (mState != authState.value)
323
+ throw new Error('state does not match')
324
+
325
+ let url = ''
326
+
327
+ options.getTokenUrl ??= () => {
328
+ return appendUrl(useAuthUrl(), 'token')
329
+ }
330
+
331
+ url = options.getTokenUrl(mCode, mRedirect, mGrantType, mClientID)
332
+
333
+ const formData: any = {}
334
+ formData['grant_type'] = mGrantType
335
+ formData['code'] = mCode
336
+ formData['redirect_uri'] = `${window.location.origin}/authentication`
337
+ formData['client_id'] = mClientID
338
+
339
+ const res = await fetch(url, {
340
+ method: 'POST',
341
+ mode: 'cors',
342
+ cache: 'no-cache',
343
+ body: JSON.stringify(formData)
344
+ })
345
+
346
+ const data = await res.json()
347
+
348
+ setAuth(data.value?.access_token)
349
+ }
350
+
351
+ return await getToken(code, state, redirect_path)
352
+ }
353
+
297
354
  /**if not demoing, then will decrypt the jwtToken and attempt to set credentials */
298
355
  function setAuth(jwtToken?: string) {
299
356
  if (jwtToken == null) return
@@ -303,9 +360,8 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
303
360
 
304
361
  setDefaultAuth(d)
305
362
 
306
- if (options.setCredentials != null) {
307
- options.setCredentials(state as RemovableRef<any>, d)
308
- }
363
+ if (options.processTokenPayload != null)
364
+ options.processTokenPayload(state, d)
309
365
  }
310
366
  }
311
367
 
@@ -341,7 +397,7 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
341
397
  //what if no internet?
342
398
  if (mState.isLoggedIn && tokenExpired()) {
343
399
  logout()
344
- window.location.href = getAuthUrl()
400
+ window.location.href = getAuthorizeUrl()
345
401
  }
346
402
 
347
403
  return mState.isLoggedIn
@@ -367,8 +423,9 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
367
423
  credentials: state.value,
368
424
  doShow,
369
425
  doShowByNav,
370
- getAuthUrl,
426
+ getAuthorizeUrl,
371
427
  getTimeZone,
428
+ getToken,
372
429
  login,
373
430
  logout,
374
431
  setAuth,
package/src/core.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { App } from 'vue'
2
2
  import { createApi } from './composables/api'
3
- import { createAuth, type AuthSubscription } from './composables/auth'
3
+ import { CreateAuthOptions, createAuth, type AuthSubscription } from './composables/auth'
4
4
  import { BaseCosmeticTheme, createCosmetics, UseCosmeticsOptions } from './composables/cosmetics'
5
5
  import { createDates } from './composables/dates'
6
6
  import { createDemo } from './composables/demo'
@@ -10,7 +10,6 @@ import { createPresets } from './composables/presets'
10
10
  import { createPWA } from './composables/pwa'
11
11
  import { createStoreBuilder } from './composables/stores'
12
12
  import { CreateUrlOptions, createUrl } from './composables/urls'
13
- import { RemovableRef } from '@vueuse/core'
14
13
 
15
14
 
16
15
  import BTBtn from './components/BT-Btn.vue'
@@ -36,11 +35,12 @@ export interface CoreApp {
36
35
  }
37
36
 
38
37
  export interface CreateCoreOptions extends UseCosmeticsOptions<BaseCosmeticTheme> {
38
+ auth: CreateAuthOptions
39
39
  defaultCacheExpiryHours?: number
40
- getAuthQuery: (redirectPath?: string, state?: string) => string
40
+ // getAuthQuery: (redirectPath?: string, state?: string) => string
41
41
  navItems?: NavigationItem[]
42
42
  presets: any
43
- setCredentials?: (state: RemovableRef<any>, payload: any) => void
43
+ // setCredentials?: (state: RemovableRef<any>, payload: any) => void
44
44
  /**suboptions */
45
45
  subscriptionOptions?: AuthSubscription[]
46
46
  urls: CreateUrlOptions
@@ -50,7 +50,6 @@ export function createCore(options: CreateCoreOptions): CoreApp {
50
50
 
51
51
  return {
52
52
  install(app: App) {
53
- // const core = this
54
53
 
55
54
  //register components
56
55
  app.component('bt-btn', BTBtn)
@@ -82,13 +81,17 @@ export function createCore(options: CreateCoreOptions): CoreApp {
82
81
 
83
82
  createPresets(options)
84
83
 
85
- const auth = createAuth({
86
- demo: demo,
87
- getAuthItem: navigation.findItem,
88
- getAuthQuery: options.getAuthQuery,
89
- setCredentials: options.setCredentials,
90
- subscriptionOptions: options.subscriptionOptions
91
- })
84
+ options.auth.demo ??= demo
85
+
86
+ const auth = createAuth(options.auth)
87
+
88
+ // {
89
+ // demo: demo,
90
+ // getAuthItem: navigation.findItem,
91
+ // getAuthorizeUrl: options.auth
92
+ // setCredentials: options.setCredentials,
93
+ // subscriptionOptions: options.subscriptionOptions
94
+ // })
92
95
 
93
96
  const api = createApi({
94
97
  auth: auth,