
    wg\jk.                     F   S SK r S SKrS SKrS SKrS SKrS SKr\ R                  " \5      r\R                  " S5      r
S r " S S5      r " S S\5      r " S S	\5      r " S
 S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)    NzW(?<=^)"([^"]+)"(?=:|$)|(?<=:)"([^"]+)"(?=:|$)|(?<=^)([^:]*)(?=:|$)|(?<=:)([^:]*)(?=:|$)c                     [         R                  U 5      nU Vs/ s H+  o"S   =(       d    US   =(       d    US   =(       d    US   PM-     sn$ s  snf )zIt works like src.split(":") but with the ability to use a colon
if you wrap the word in quotation marks.

a:b:c:d -> ['a', 'b', 'c', 'd'
a:"b:c":c -> ['a', 'b:c', 'd']
r            )_SOURCE_SPLIT_REGEXfindall)srcmatchesms      S/home/edenadmin/noVNC/venv/lib/python3.13/site-packages/websockify/token_plugins.pyparse_source_argsr      sL     "))#.G29:'QaD(AaD(AaD(AaD(':::s   2Ac                        \ rS rSrS rS rSrg)
BasePlugin   c                     Xl         g Nsource)selfr	   s     r   __init__BasePlugin.__init__   s        c                     g r    r   tokens     r   lookupBasePlugin.lookup!   s    r   r   N)__name__
__module____qualname____firstlineno__r   r   __static_attributes__r   r   r   r   r      s    r   r   c                   4   ^  \ rS rSrU 4S jrS rS rSrU =r$ )ReadOnlyTokenFile%   c                 4   > [         TU ]  " U0 UD6  S U l        g r   )superr   _targetsr   argskwargs	__class__s      r   r   ReadOnlyTokenFile.__init__)   s    $)&)r   c                    [         R                  R                  U R                  5      (       aY  [         R                  " U R                  5       Vs/ s H-  n[         R                  R                  U R                  U5      PM/     nnOU R                  /n0 U l        SnU H  n[        U5      R                  5        Vs/ s H  oDR                  5       PM     sn Hl  nU(       a]  UR                  S5      (       dG   [        R                  " SU5      u  pgUR                  5       R                  SS5      U R                  U'   US-  nMn     M     g s  snf s  snf ! [         a'    [        R!                  SU R                  U4-  5         NHf = f)Nr   #z:\s:zSyntax error in %s on line %d)ospathisdirr   listdirjoinr)   open	readlinesstrip
startswithresplitrsplit
ValueErrorloggererror)r   f	cfg_filesindexllinetoktargets           r   _load_targetsReadOnlyTokenFile._load_targets-   s:   77==%% jj575 dkk155  7I IA,0G,=,=,?@,?q,?@ 4 4]&(hhvt&<-3\\^-B-B3-Jc* 
 A 7 A
 & ]%DUZG[%[\]s   4E>E;AE.F
	F
c                 z    U R                   c  U R                  5         XR                   ;   a  U R                   U   $ g r   )r)   rH   r   s     r   r   ReadOnlyTokenFile.lookup@   s4    ==  MM!==''r   )r)   )	r   r    r!   r"   r   rH   r   r#   __classcell__r-   s   @r   r%   r%   %   s    & r   r%   c                   (   ^  \ rS rSrU 4S jrSrU =r$ )	TokenFileM   c                 B   > U R                  5         [        TU ]	  U5      $ r   )rH   r(   r   )r   r   r-   s     r   r   TokenFile.lookupQ   s    w~e$$r   r   )r   r    r!   r"   r   r#   rL   rM   s   @r   rO   rO   M   s    % %r   rO   c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )TokenFileNameV   c                    > [         TU ]  U5        [        R                  R	                  U5      (       d  [        S5      eg )Nz)TokenFileName plugin requires a directory)r(   r   r2   r3   r4   	Exception)r   r	   r-   s     r   r   TokenFileName.__init__Z   s4    ww}}S!!GHH "r   c                 J   [         R                  R                  U5      n[         R                  R                  U R                  U5      n[         R                  R                  U5      (       a6  [        U5      R                  5       R                  5       R                  S5      $ g )Nr1   )
r2   r3   basenamer6   r   existsr7   readr9   r<   )r   r   r3   s      r   r   TokenFileName.lookup_   si      'ww||DKK/77>>$:??$**,22377r   r   r   r    r!   r"   r   r   r#   rL   rM   s   @r   rT   rT   V   s    I
 r   rT   c                        \ rS rSrS rS rSrg)BaseTokenAPIh   c                 f    UR                   R                  S5      u  p#UR                  SS5      nX#/$ )Nr1   asciiignore)textr<   encode)r   resphostports       r   process_resultBaseTokenAPI.process_resulto   s.    YY__S)
{{78,~r   c                     SS K nUR                  U R                  U-  5      nUR                  (       a  U R	                  U5      $ g )Nr   )requestsgetr   okrj   )r   r   rm   rg   s       r   r   BaseTokenAPI.lookupt   s7    ||DKK%/077&&t,,r   r   N)r   r    r!   r"   rj   r   r#   r   r   r   r`   r`   h   s    
r   r`   c                       \ rS rSrS rSrg)JSONTokenApi   c                 6    UR                  5       nUS   US   4$ )Nrh   ri   )json)r   rg   	resp_jsons      r   rj   JSONTokenApi.process_result   s"    IIK	&!9V#455r   r   N)r   r    r!   r"   rj   r#   r   r   r   rr   rr      s    6r   rr   c                       \ rS rSrS rSrg)JWTTokenApi   c                 Z    SSK JnJn  SS KnUR	                  5       n [        U R                  S5       nUR                  5       nS S S 5         UR                  W5         UR                  XQS	9nUR                   " UR"                  5      n	S
U	;   a  UR                  XQR$                  S	9nUR                   " UR$                  5      n
SU
;   a2  [&        R&                  " 5       U
S   :  a  [        R)                  S5        g SU
;   a2  [&        R&                  " 5       U
S   :  a  [        R)                  S5        g U
S   U
S   4$ ! , (       d  f       GN= f! [         a+  n[        R                  S[        U5      -  5         S nAg S nAff = f!    UR                  WR                  S5      SS9   GNP!   [        R                  S5          g = f= f! [         a+  n[        R                  S[        U5      -  5         S nAg S nAff = f! [*         a    [        R                  S5         g f = f)Nr   )jwtjwkrbzError loading key file: %sutf-8oct)kktyz#Failed to correctly parse key data!)keyr|   encnbfzToken can not be used yet!expzToken has expired!rh   ri   zFailed to parse token: %szGpackage jwcrypto not found, are you sure you've installed it correctly?)jwcryptor|   r}   ru   JWKr7   r   r\   rW   r?   r@   strimport_from_pem
import_keydecodeJWTloadsheaderclaimstimewarningImportError)r   r   r|   r}   ru   r   key_filekey_dataeparsed_headerparseds              r   r   JWTTokenApi.lookup   s   2	)'')C$++t,'}}H - ##H-C3 $

5<< 8M)GGG>EELL1F?yy{VE]2'CD#F?yy{VE]2';<#vv77K -, 9CFBC  NNX__W%=%NH LL!FG2  83q6AB  	LLbc	s   H E EE F !BG ?7G 7	G 
EE H E 
F!F>H FH GF0-H 0G
GH 
GH 
H!H ;H  HH H*)H*r   N)r   r    r!   r"   r   r#   r   r   r   ry   ry      s    3r   ry   c                   $    \ rS rSrSrS rS rSrg)
TokenRedis   aW  Token plugin based on the Redis in-memory data store.

The token source is in the format:

    host[:port[:db[:password[:namespace]]]]

where port, db, password and namespace are optional. If port or db are left empty
they will take its default value, ie. 6379 and 0 respectively.

If your redis server is using the default port (6379) then you can use:

    my-redis-host

In case you need to authenticate with the redis server and you are using
the default database and port you can use:

    my-redis-host:::verysecretpass

You can also specify a namespace. In this case, the tokens
will be stored in the format '{namespace}:{token}'

    my-redis-host::::my-app-namespace

Or if your namespace is nested, you can wrap it in quotes:

    my-redis-host::::"first-ns:second-ns"

In the more general case you will use:

    my-redis-host:6380:1:verysecretpass:my-app-namespace

The TokenRedis plugin expects the format of the target in one of these two
formats:

- JSON

    {"host": "target-host:target-port"}

- Plain text

    target-host:target-port

Prepare data with:

    redis-cli set my-token '{"host": "127.0.0.1:5000"}'

Verify with:

    redis-cli --raw get my-token

Spawn a test "server" using netcat

    nc -l 5000 -v

Note: This Token Plugin depends on the 'redis' module, so you have
to install it before using this plugin:

      pip install redis
c                     SS K nSU l        SU l        S U l        SU l	         [        U5      n[        U5      S:X  a  US   U l        GO[        U5      S:X  a)  Uu  U l        U l        U R                  (       d  SU l        GO`[        U5      S:X  aG  Uu  U l        U l        U l        U R                  (       d  SU l        U R                  (       d  SU l        GO
[        U5      S:X  ad  Uu  U l        U l        U l        U l        U R                  (       d  SU l        U R                  (       d  SU l        U R                  (       d  S U l        O[        U5      S	:X  a  Uu  U l        U l        U l        U l        U l	        U R                  (       d  SU l        U R                  (       d  SU l        U R                  (       d  S U l        U R                  (       d  SU l	        O[        e[        U R                  5      U l        [        U R                  5      U l        U R                  (       a  U =R                  S
-  sl	        [        R                  SU R                  < S
U R                  < S35        g ! [         a.    [        R                  S5        [        R
                  " 5          GNf = f! [         a0    [        R                  SU-  5        [        R
                  " 5          g f = f)Nr   zUnable to load redis modulei   r   r   r         r1   z TokenRedis backend initialized ()zpThe provided --token-source='%s' is not in the expected format <host>[:<port>[:<db>[:<password>[:<namespace>]]]])redisr   r?   r@   sysexit_port_db	_password
_namespacer   len_serverr>   intinfo)r   r	   r   fieldss       r   r   TokenRedis.__init__   s   	
 
-	&s+F6{a%ayV!+1(djzz!%DJV!5;2dj$(zz!%DJxx DHV!EKBdj$(DNzz!%DJxx DH~~%)DNV!V\Sdj$(DNDOzz!%DJxx DH~~%)DN&(DO  TZZDJ488}DH3&KK<<- ._  	LL67HHJ	b  	LL ]  HHJ		s#   I. I
J) .4J&%J&)7K#"K#c                     SS K n[        R                  SU-  5        WR                  U R                  U R                  U R                  U R                  S9nUR                  U R                  U-   5      nUc  g UR                  S5      R                  5       n[        R!                  SU-  5        UR#                  S5      (       a.   [$        R&                  " U5      nUS   R)                  S	5      u  pxOI[.        R0                  " SU5      (       a  UR)                  S	5      u  pxO[        R                  SU-  5        g [        R!                  SU< SU< 35        Xx/$ ! [         a.    [        R                  S5        [        R
                  " 5          GNf = f! [*         a    [        R                  S
U-  5         g [,         a    [        R                  SU-  5         g f = f)Nr   zFpackage redis not found, are you sure you've installed them correctly?zresolving token '%s')rh   ri   dbpasswordr   zresponse from redis : %s{rh   r1   zUnable to decode JSON token: %sz+Unable to find 'host' key in JSON token: %sz\S+:\S+zUnable to parse token: %szhost: z, port: )r   r   r?   r@   r   r   r   Redisr   r   r   r   rn   r   r   r9   debugr:   ru   r   r<   r>   KeyErrorr;   match)	r   r   r   clientstuffresponseStrcomborh   ri   s	            r   r   TokenRedis.lookup8  s   	
 	*U23$,,TZZ $4>>  C

4??U23=,,w/557KLL3kAB%%c**
  JJ{3E!&v!4!4S!9JD$ *k22(..s3
d8;FGLLt<=<?  	LLabHHJ	" "  LL!B!,"- .  LL!N!,"- . s)   E ,F 4FF"G!=!G! G!)r   r   r   r   r   N)r   r    r!   r"   __doc__r   r   r#   r   r   r   r   r      s    :v8t" r   r   c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )UnixDomainSocketDirectoryi]  c                    > [         TU ]  " U0 UD6  [        R                  R	                  U R
                  5      U l        g r   )r(   r   r2   r3   abspathr   	_dir_pathr*   s      r   r   "UnixDomainSocketDirectory.__init__^  s-    $)&)5r   c                 f    SS K n[        R                  R                  U R                  5      (       d  g [        R                  R                  [        R                  R                  U R                  U5      5      nUR                  U R                  5      (       d  g [        R                  R                  U5      (       d  g UR                  [        R                   " U5      R                  5      (       d  g SU/$ ! [         a+  n[        R                  S[        U5      -  5         S nAg S nAff = f)Nr   unix_socketz$Error finding unix domain socket: %s)statr2   r3   r4   r   r   r6   r:   r[   S_ISSOCKst_moderW   r?   r@   r   )r   r   r   uds_pathr   s        r   r    UnixDomainSocketDirectory.lookupb  s    	77==00wwrww||DNNE'JKH&&t~~6677>>(++==!2!:!:;;"H.. 	Cc!fLM	s/   2C; A'C; $C; 4C; 7C; ;
D0!D++D0)r   r^   rM   s   @r   r   r   ]  s    6 r   r   )loggingr2   r   r   r;   ru   	getLoggerr   r?   compiler   r   r   r%   rO   rT   r`   rr   ry   r   r   r   r   r   <module>r      s     	 
  	 			8	$jj ; "
 "P%! %J $: .6< 67* 7tX  X v
 r   