@@ -58,7 +58,6 @@ public async Task SetCredentials(string coderUrl, string apiToken, CancellationT
5858 if ( coderUrl . Length > 128 ) throw new ArgumentOutOfRangeException ( nameof ( coderUrl ) , "Coder URL is too long" ) ;
5959 if ( ! Uri . TryCreate ( coderUrl , UriKind . Absolute , out var uri ) )
6060 throw new ArgumentException ( $ "Coder URL '{ coderUrl } ' is not a valid URL", nameof ( coderUrl ) ) ;
61- if ( uri . Scheme != "https" ) throw new ArgumentException ( "Coder URL must use HTTPS" , nameof ( coderUrl ) ) ;
6261 if ( uri . PathAndQuery != "/" ) throw new ArgumentException ( "Coder URL must be the root URL" , nameof ( coderUrl ) ) ;
6362 if ( string . IsNullOrWhiteSpace ( apiToken ) ) throw new ArgumentException ( "API token is required" , nameof ( apiToken ) ) ;
6463 apiToken = apiToken . Trim ( ) ;
@@ -105,11 +104,15 @@ public void ClearCredentials()
105104
106105 private void UpdateState ( CredentialModel newModel )
107106 {
108- _latestCredentials = newModel ;
109- CredentialsChanged ? . Invoke ( this , _latestCredentials . Clone ( ) ) ;
107+ using ( _lock . Lock ( ) )
108+ {
109+ _latestCredentials = newModel . Clone ( ) ;
110+ }
111+
112+ CredentialsChanged ? . Invoke ( this , newModel . Clone ( ) ) ;
110113 }
111114
112- private RawCredentials ? ReadCredentials ( )
115+ private static RawCredentials ? ReadCredentials ( )
113116 {
114117 var raw = NativeApi . ReadCredentials ( CredentialsTargetName ) ;
115118 if ( raw == null ) return null ;
@@ -130,7 +133,7 @@ private void UpdateState(CredentialModel newModel)
130133 return credentials ;
131134 }
132135
133- private void WriteCredentials ( RawCredentials credentials )
136+ private static void WriteCredentials ( RawCredentials credentials )
134137 {
135138 var raw = JsonSerializer . Serialize ( credentials ) ;
136139 NativeApi . WriteCredentials ( CredentialsTargetName , raw ) ;
@@ -147,6 +150,7 @@ private static class NativeApi
147150 private const int CredentialTypeGeneric = 1 ;
148151 private const int PersistenceTypeLocalComputer = 2 ;
149152 private const int ErrorNotFound = 1168 ;
153+ private const int CredMaxCredentialBlobSize = 5 * 512 ;
150154
151155 public static string ? ReadCredentials ( string targetName )
152156 {
@@ -170,15 +174,17 @@ private static class NativeApi
170174
171175 public static void WriteCredentials ( string targetName , string secret )
172176 {
173- if ( Encoding . Unicode . GetByteCount ( secret ) > 512 )
174- throw new ArgumentOutOfRangeException ( nameof ( secret ) , "The secret is greater than 512 bytes" ) ;
177+ var byteCount = Encoding . Unicode . GetByteCount ( secret ) ;
178+ if ( byteCount > CredMaxCredentialBlobSize )
179+ throw new ArgumentOutOfRangeException ( nameof ( secret ) ,
180+ $ "The secret is greater than { CredMaxCredentialBlobSize } bytes") ;
175181
176182 var credentialBlob = Marshal . StringToHGlobalUni ( secret ) ;
177183 var cred = new CREDENTIAL
178184 {
179185 Type = CredentialTypeGeneric ,
180186 TargetName = targetName ,
181- CredentialBlobSize = secret . Length * sizeof ( char ) ,
187+ CredentialBlobSize = byteCount ,
182188 CredentialBlob = credentialBlob ,
183189 Persist = PersistenceTypeLocalComputer ,
184190 } ;
0 commit comments