@@ -10,16 +10,22 @@ import { WebhookStoreUrlContext } from "../WebhookStoreUrl/WebhookStoreUrl.conte
1010import { ACCESS_TOKEN_KEY , IDENTITY_TOKEN_KEY } from "../../local-storage" ;
1111import { decodeJWT } from "../../utils/decode-jwt" ;
1212
13+ export type AuthMetadata =
14+ | { protected : true ; protectionRule : "hostname webhook.store" }
15+ | { protected : true ; protectionRule : "github-org" ; ghOrg : string }
16+ | { protected : false } ;
17+
1318export const StoreConfigNavItem = ( ) => {
1419 const [ isClicked , setClicked ] = useState < boolean > ( false ) ;
1520
16- const [ authConfig , setAuthConfig ] = useState < { protected : boolean } > ( {
21+ const [ authConfig , setAuthConfig ] = useState < AuthMetadata > ( {
1722 protected : false ,
1823 } ) ;
1924 const [ storeConfig , setStoreConfig ] = useState < {
2025 maxNumberOfWebhookPerHost ?: number ;
2126 defaultTarget ?: string [ ] ;
22- } > ( { } ) ;
27+ userHasAccessToStore : boolean ;
28+ } > ( { userHasAccessToStore : false } ) ;
2329
2430 const { value : webhookStoreUrl } = useContext ( WebhookStoreUrlContext ) ;
2531 const accessToken = localStorage . getItem ( ACCESS_TOKEN_KEY ) ;
@@ -32,32 +38,29 @@ export const StoreConfigNavItem = () => {
3238 decodeJWT < { name : string ; ghOrganisations : string [ ] } , any > ( idToken ) ;
3339
3440 useEffect ( ( ) => {
35- getAuthConfig ( ) ;
36- getStoreConfig ( ) ;
41+ getConfigs ( ) ;
3742 } , [ ] ) ;
3843
39- async function getAuthConfig ( ) {
44+ async function getConfigs ( ) {
4045 const initialiseAuthConfig = await get ( "auth-metadata" ) ;
4146 if ( response . ok ) setAuthConfig ( initialiseAuthConfig ) ;
42- }
43-
44- async function getStoreConfig ( ) {
4547 const initialiseStoreConfig = await get ( "store-metadata" ) ;
46- if ( response . ok ) setStoreConfig ( initialiseStoreConfig ) ;
48+ if ( response . ok )
49+ setStoreConfig ( { ...initialiseStoreConfig , userHasAccessToStore : true } ) ;
4750 }
4851
49- const accessConfig = {
50- type : authConfig . protected ? "private" : "public" ,
51- sublabel : authConfig . protected ? "Only you" : "Anyone with the link" ,
52- } as const ;
52+ const accessConfig = describeAccessFromAuthConfig (
53+ authConfig ,
54+ webhookStoreUrl
55+ ) ;
5356 const availableStores = identityToken
5457 ? [
5558 {
5659 url : `https://${ identityToken . payload . name } .github-org.webhook.store/?access_token=${ idToken } ` ,
5760 display : `${ identityToken . payload . name } .github-org.webhook.store` ,
5861 } ,
5962 ...identityToken . payload . ghOrganisations . map ( ( orgName ) => ( {
60- url : `https://${ orgName } .github-org .webhook.store/?access_token=${ idToken } ` ,
63+ url : `https://${ orgName } .github.webhook.store/?access_token=${ idToken } ` ,
6164 display : `${ orgName } .github-org.webhook.store` ,
6265 } ) ) ,
6366 ]
@@ -79,6 +82,7 @@ export const StoreConfigNavItem = () => {
7982 availableStores = { availableStores }
8083 defaultTargets = { defaultTargets }
8184 storageLimit = { storageLimit }
85+ userHasAccessToStore = { storeConfig . userHasAccessToStore }
8286 />
8387 </ Dialog >
8488 }
@@ -88,8 +92,49 @@ export const StoreConfigNavItem = () => {
8892 appearance = { Button . appearances . flat }
8993 onClick = { ( _ ) => setClicked ( ! isClicked ) }
9094 >
91- < Label size = { Label . sizes . xSmall } > Store Config ⚠️</ Label >
95+ < Label size = { Label . sizes . xSmall } >
96+ Store Config { authConfig . protected ? null : "⚠️" }
97+ </ Label >
9298 </ Button >
9399 </ Below >
94100 ) ;
95101} ;
102+
103+ const describeAccessFromAuthConfig = (
104+ authConfig : AuthMetadata ,
105+ webhookStoreUrl : string
106+ ) : { type : "public" | "private" ; sublabel : string } => {
107+ if ( ! authConfig . protected ) {
108+ return { type : "public" , sublabel : "Anyone with the link" } ;
109+ }
110+
111+ if ( authConfig . protectionRule === "github-org" ) {
112+ return {
113+ type : "private" ,
114+ sublabel : `Only members of ${ authConfig . ghOrg } on GitHub` ,
115+ } ;
116+ }
117+
118+ if ( authConfig . protectionRule === "hostname webhook.store" ) {
119+ const webhookStoreDomain = new URL ( webhookStoreUrl ) . hostname ;
120+ if ( webhookStoreDomain . endsWith ( ".github.webhook.store" ) ) {
121+ const githubUserName =
122+ webhookStoreDomain . split ( "." ) [ webhookStoreDomain . split ( "." ) . length - 4 ] ;
123+ return {
124+ type : "private" ,
125+ sublabel : `Only Github user ${ githubUserName } ` ,
126+ } ;
127+ }
128+ if ( webhookStoreDomain . endsWith ( ".github-org.webhook.store" ) ) {
129+ const githubOrgaName =
130+ webhookStoreDomain . split ( "." ) [ webhookStoreDomain . split ( "." ) . length - 4 ] ;
131+ return {
132+ type : "private" ,
133+ sublabel : `Only members of ${ githubOrgaName } on GitHub` ,
134+ } ;
135+ }
136+ return { type : "public" , sublabel : "Anyone with the link" } ;
137+ }
138+
139+ return { type : "public" , sublabel : "Anyone with the link" } ;
140+ } ;
0 commit comments