Hi, the I'm using the following code to get the code_verifier and code_challenge strings for an OAUTH2 authentication, which wasn't working but unknown to me because until a few days ago, the code_verifier string was well, never verified by the authenticator (duh!). Now it does this verification so I need to fix the code to make it work for real
_code_verifier = StringUtil.convertEncodedString(Cryptography.randomBytes(86/2), { :fromRepresentation => StringUtil.REPRESENTATION_BYTE_ARRAY, :toRepresentation => StringUtil.REPRESENTATION_STRING_HEX, }); var code_verifier_bytes = StringUtil.convertEncodedString(_code_verifier, { :fromRepresentation => StringUtil.REPRESENTATION_STRING_PLAIN_TEXT, :toRepresentation => StringUtil.REPRESENTATION_BYTE_ARRAY, }); var hmac = new Cryptography.HashBasedMessageAuthenticationCode({ :algorithm => Cryptography.HASH_SHA256, :key => code_verifier_bytes }); var code_challenge = StringUtil.convertEncodedString(hmac.digest(), { :fromRepresentation => StringUtil.REPRESENTATION_BYTE_ARRAY, :toRepresentation => StringUtil.REPRESENTATION_STRING_BASE64, });
Reason I say it doesn't work is two fold. First, well the authentication process now returns 400 with "invalid code_verifier" and when I enter the code_verifier in online converters like https://tonyxu-io.github.io/pkce-generator/ and https://referbruv.com/utilities/pkce-generator-online/, I get a different answer than them for code_challenge. Anybody knows what's wrong with the code because myself, I've pound at it for a few hours and still come empty :-( I'm using ConnecytIQ 4.1.5 SDK if it matters but I tried a few and get the same result (I didn't try more recent ones because of type definition errors that I'll tackle another time).
BTW, I know I need to make the code_challenge URL friendly, which I do later but it shouldn't affect what is returned (beside for + / and = characters, which are replaced with - _ and 'skipped').
Thanks.