<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, 14 Jun 2020 at 01:40, Tim Johnson <<a href="mailto:digit@sonic.net">digit@sonic.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><br><div><br><blockquote type="cite"><div>On Jun 11, 2020, at 6:39 PM, Levente Uzonyi <<a href="mailto:leves@caesar.elte.hu" target="_blank">leves@caesar.elte.hu</a>> wrote:</div><br><div><div><blockquote type="cite" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">It is useful for SSO.  But for automated workflows, where human intervention is undesired, there is another type of OAuth2 which may be called "server to server" or apparently "two-legged OAuth":<br><a href="https://developers.google.com/identity/protocols/oauth2/service-account" target="_blank">https://developers.google.com/identity/protocols/oauth2/service-account</a><br>I tried and failed to implement this last year.  Where I failed was in computing the JSON Web Signature (JWS).  I could generate JWTs successfully*, but JWSs for Google require "SHA256withRSA (also known as<br>RSASSA-PKCS1-V1_5-SIGN with the SHA-256 hash function)" which requires one very specific algorithm missing from SqueakSSL and which I simply could not engineer on my own at the time (or perhaps ever — I might just not be<br>smart enough ;) ).  I was able to get as far as crafting the JSON in Squeak and then signing using Python's implementation of the algorithm and it would work.<br>If we could get server-to-server OAuth2 using WebClient, that would also allow us to, say, connect to Google Drive directly from Squeak, or be a client of Google Cloud Platform / Compute Engine, etc.  I think that would be<br>very cool.<br>Sadly, I seem to recall this type of OAuth (and thus this algorithm) would also be necessary for GitHub/GitLab.<br></blockquote><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline">The Cryptography package seems to have this stuff implemented, though I haven't verified the results. Here's how to use it:</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline">Let's say privateKey is an RSAPrivateKey with your private key (See class side methods and RSAPrivateKeyFileReader how to initialize it) and message is your serialized json to sign. Then</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:pre-wrap;word-spacing:0px;text-decoration:none">  </span><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline">privateKey v15SignMessageHash: (HashFunction newSHA256 digestInfoAsn1DerEncodingFromMessage: message)</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline">should return the signature.</span></div></div></blockquote><br></div><div>Thank you Levente.  When I mentioned algorithms I was hoping you might respond.  :) </div><div><br></div><div>I found my old JWT code from last year on GitHub:  <a href="https://github.com/tcj/beaufort" target="_blank">https://github.com/tcj/beaufort</a>      (Oh no, it contains an old email address of mine in one of its tests...)</div><div><br></div><div>I loaded Cryptography into an image so I could run my tests again, and noticed that the Cryptography package has seen a lot of work this year.  Thanks for that.</div><div><br></div><div>So:  the tests I wrote last year (with a small change) pass now.  This could be very good news and may be worth further investigation.  Thank you again!  I will update my tests (& remove my old email address...?) and upload a new version of this package.</div><div><br></div><div>I am reminded that the issue I'd encountered last year was that HS256 could work, but RS256 could not.  Now, unit tests are passing for both.  (My BfJWTRFCTest>>#testConversion is not passing so I'll need to look into that.)</div><div><br></div><div>Here is the gist of my JWT signing code:</div><div><br></div><div><div>BfJWT>>#signedWith: aKey</div><div><span style="white-space:pre-wrap">     </span>| headerAndClaims |</div><div><span style="white-space:pre-wrap">      </span>self secret: aKey.</div><div><span style="white-space:pre-wrap">       </span>headerAndClaims := self headerAndClaims.</div><div><span style="white-space:pre-wrap"> </span>^ '{1}.{2}' format: { headerAndClaims . self signatureFrom: headerAndClaims }</div><div><br></div><div>... #signatureFrom: is implemented differently for HS256 versus RS256 subclasses of BfJWT.  My RS256 encoding was like this:</div><div><br></div><div><div>BfRS256 signatureFrom: aString</div><div><span style="white-space:pre-wrap">      </span>| signedMessage hashed privateKey |</div><div><span style="white-space:pre-wrap">      </span>privateKey := (Pkcs12PrivateKeyFileReader fromFile: 'timj-project-mar-2019-a94d67a8d0c8.p12') asPrivateKey.</div><div><span style="white-space:pre-wrap">      </span>signedMessage := privateKey signMessage: aString.</div><div><span style="white-space:pre-wrap">        </span>hashed := SHA256 new hashStream: signedMessage readStream.</div><div><span style="white-space:pre-wrap">       </span>^ hashed base64UrlEncoded</div></div></div><div><br></div><div>A year ago, I could have described how & why it wasn't working... but that information has left my brain now.</div><div><br></div><div>For what it's worth, Norbert Hartl's JSONWebToken also lacks support for this RS256 encoding format.</div><div><br></div><div><a href="https://github.com/noha/JSONWebToken/commit/4a4d20eaa6e84e2676a577f74bc6e24c1ead0047" target="_blank">https://github.com/noha/JSONWebToken/commit/4a4d20eaa6e84e2676a577f74bc6e24c1ead0047</a></div><div><br></div><div>It seems to be a common issue around the internet that people find RS256 very difficult.</div></div></blockquote><div><br></div><div>Just to round out options (but note, this was using Pharo)...</div><div><br></div><div>A couple of months ago I needed JWT RS256 for Google OAuth.  </div><div>Asking around I was advised that it was working in the following branch...<br></div><div><a href="https://github.com/noha/JSONWebToken/pull/7">https://github.com/noha/JSONWebToken/pull/7</a>  <br></div><div><br></div><div>Although I didn't end up using that directly, but got JWT RS256 working as part of...<br><a href="https://github.com/psvensson/cloudsdk-st/blob/master/CloudConversations-Core/CCGoogleConnectionTest.class.st">https://github.com/psvensson/cloudsdk-st/blob/master/CloudConversations-Core/CCGoogleConnectionTest.class.st</a>  <br></div><div><br></div><div>cheers -ben</div><div></div></div></div>