Wednesday, October 20, 2021
IndexedDB can possibly fail on FireFox
Friday, October 1, 2021
tiny-totp.js, a tiny TOTP javascript implementation
Just published the tiny-totp.js code on Github. It's super tiny Javascript RFC6328 implementation. If you wonder what's that, it's the Timed One-Time Passwords specs that is widely used to implement two-factor authentication over web.
What this tiny library does is it computes one time passwords, given the master password. It's basically the same computation Google Authenticator or Microsoft Authenticator do, given the master password. It means that you can either implement your own client or even your own server side authentication that would be compatible with existing authenticators (including the two).
What's interesting here is that the implementation is really, really tiny and has 0 dependencies. Usually, people implement TOTP using two dependencies:
- to handle the base32 encoding (master keys are usually provided in base32 encoding
- to handle the hmacsha1 to calculate actual codes
Thursday, August 26, 2021
TypeScript puzzle No.1 (intermediate)
type Callable = {
description: string;
(a: string): string;
}
It's pretty obvious that if given an instance of the type, one can just call it:
function CallableClient( c: Callable, s: string ): string {
return c(s);
}
The question is however, how to create instance of the type so that it's both callable and has the string property and the creation is strongly typed (doesn't involve any type). Formally, your task is to implement the factory method that takes a function, a description and composes them:
function F2Factory( f: (a:string) => string, description: string ): Callable {
return ...; // ??
}
This is actually pretty interesting so here's goes the story. There are two possible approaches I am aware of.
First approach involves a const that is further expanded with an attribute:
const c : Callable = (s: string) => s; c.description = "foo"; // c is correctly of Callable type and can be calledNote that it only work when the variable is declared as const, doesn't work with var/let. This should be enough to provide the implementation of the F2Factory
Second approach involves Object.assign which is typed as Object.assign<T, U>( t: T, u: U ) : T & U. It's great as it looks like it could just combine two objects and return a new object that act as both. Of course then, this works
function F2Factory( f: (a:string) => string, description: string ): Callable {
return Object.assign(f, { description });
}
const c = F2Factory( (s: string) => s, 'foo' );
console.log( c('bar') );
console.log( c.description );
Note, however, that there's a caveat here. The Object.assign basically duplicates attributes of the source to the target. However, a callable object (a function) doesn't have any properties that can be duplicated
to another object so that the other object would become callable too. This doesn't work then:
function F2Factory( f: (a:string) => string, description: string ): Callable {
return Object.assign({ description }, f);
}
const c = F2Factory( (s: string) => s, 'foo' );
console.log( c('bar') );
console.log( c.description );
This could be surprising for someone, it doesn't work even though it types correctly!
The reason here is that for the typescript compiler, using Object.assign to combine a function and an object in any order produces the same output type. However, for the Javascript engine that actually runs the code, adding description to a function works but trying to duplicate callability to a simple object { description } by no means makes this object callable.
This discrepancy between the Typescript compiler and the Javascript runtime would be even more clear if we consider a type that combines two callable members
type Callable2 = {
(a: number): number;
(a: string): string;
}
This time the Object.assign will never yield expected output - it can't combine two functions to create yet another function that correctly handles input argument type.
Friday, July 30, 2021
Javascript Puzzle No.2
let working = {
foo: function() {
console.log("working");
}
};
let notworking = {
foo() {
console.log("notworking");
}
}
new working.foo();
new notworking.foo();
and its output
Uncaught TypeError: notworking.foo is not a constructorplease explain why this particular function cannot be used as a constructor function.
Hint: based on this SO question where the explanation is provided.
Friday, June 18, 2021
PrincipalContext::ValidateCredentials doesn't always work like you think it should (false positives!)
var principalContext = new PrincipalContext(ContextType.Domain, domain); principalContext.ValidateCredentials(username, password);And this is where the problem is.
|
I am using PrincipalContext.ValidateCredentials(string, string) to validate user credentials. I am encountering behaviour that I am unable to explain and have, so far, been unable to clarify through usual searches and forum visits. Here is the behaviour, the credentials can be assumed to be correct on each call. The non-SSL behaviour is what I would expect. The SSL behaviour is not as expected in my understanding of the method.
Not using SSL/TLS
User is enabled and NOT expired - method returns TRUE User is disabled and NOT expired - method returns FALSE User is enabled and EXPIRED - method returns FALSE User is disabled and EXPIRED - Method returns FALSE
Using SSL/TLS
To summarise - when both machines in the negotiation (app server and domain controller) have the required certs installed, they will use SSL (port 636). In these circumstances the method returns TRUE for Expired accounts but when not using SSL (port 389) (for example the app server does not have the cert installed) the method is returning FALSE for Expired accounts.
I would expect the return value to be the same for expired accounts in both scenarios, but it's possible I'm not considering something.
User is enabled and NOT expired - method returns TRUE User is disabled and NOT expired - method returns FALSE User is enabled and EXPIRED - method returns TRUE User is disabled and EXPIRED - Method returns FALSE |
| Here's how ValidateCredentials(string, string) works: First, it tries to authenticate with the Negotiate, Signing, and Sealing context options. If this fails, it tries again with SimpleBind and SecureSocketLayer. |
var principalContext = new PrincipalContext(ContextType.Domain, domain); principalContext.ValidateCredentials(username, password);just make sure you call it
var principalContext = new PrincipalContext(ContextType.Domain, domain, ContextOptions.Negotiate | ContextOptions.Sealing | ContextOptions.Signing); principalContext.ValidateCredentials(username, password);This makes sure there's no SimpleBind+SSL fallback as it forces Kerberos only. And guess what? It just works correctly. Expired passwords are no longer accepted.
Tuesday, April 27, 2021
Microsoft Teams loads but renders a blank (white) window
- closing/reopening Teams
- shutting down the machine
- reinstalling Teams
- removing both ..\App Data\Local\Microsoft\Teams and ..\App Data\Roaming\Microsoft\Teams
- creating a custom link that points to Teams.exe instead of Update.exe --processStart "Teams.exe" where the default link points to
- forcing a custom power scheme for the application so that for my dual GPU machine, the correct GPU is used
Thursday, April 15, 2021
C# Puzzle No.24 (intermediate)
public interface IDoSomething: IDisposable { }
class DoesSomething<String> : IDoSomething<String>
{
public void Dispose()
{
}
}
class DoesSomething<string> : IDoSomething<string>
{
public void Dispose()
{
}
}