oauth2 icon indicating copy to clipboard operation
oauth2 copied to clipboard

Implementing "client_assertion"

Open naizerjohn-ms opened this issue 1 year ago • 15 comments

Hello!

Currently this package does not support client_assertion/client_assertion_type OAuth2.0 client authentication outlined here in the OpenID Connect documentation (not up to standard). Here is an example outlined in this documentation, for a visual on what the request would look like:

  Host: server.example.com
  Content-Type: application/x-www-form-urlencoded

  grant_type=authorization_code&
    code=i1WsRn1uB1&
    client_id=s6BhdRkqt3&
    client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion-type%3Ajwt-bearer&
    client_assertion=PHNhbWxwOl ... ZT

as opposed to what this package only currently supports (client_secret):

  Host: server.example.com
  Content-Type: application/x-www-form-urlencoded

  grant_type=authorization_code&
    code=i1WsRn1uB1&
    client_id=s6BhdRkqt3&
    client_secret=PHNhbWxwOl ... ZT

I am willing to work towards this implementation and am asking for any support/guidance for achieving this solution. Many tech companies (including ours) are migrating away from using secrets and towards more secure authentication systems. Please see this article which provides a deeper description on what client_assertions are as well.

naizerjohn-ms avatar Oct 08 '24 23:10 naizerjohn-ms

we are interested of this feature as well.

zetaab avatar Oct 14 '24 10:10 zetaab

Hi @zetaab! I see that you have made some contributions to client_assertion is the client credentials flow! However, this feature also needs to be added into the 'oauth2/internal' directory for the auth code grant flow. The auth code grant flow retrieves both an access token and an id token, the ideal grant flow for SSO (user + app). See this link for more info on this grant flow.

naizerjohn-ms avatar Oct 14 '24 16:10 naizerjohn-ms

@naizerjohn-ms that can be done already?

Example (disclaimer: I did not test this but afaik when reading code it should be possible)

	conf := &oauth2.Config{
		ClientID:     authConf.ClientID,
		Endpoint:     provider.Endpoint(),
		Scopes:       scopes,
		RedirectURL:  authConf.RedirectURI,
		AuthStyle:    oauth2.AuthStyleInParams,
	}
	oauthCtx := oidc.ClientContext(context.Background(), &http.Client{})
	otoken, err := conf.Exchange(
		oauthCtx,
		content.Code,
		oauth2.SetAuthURLParam("client_assertion", "foo"),
		oauth2.SetAuthURLParam("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"),
	)
	...

zetaab avatar Oct 14 '24 17:10 zetaab

@zetaab Great point, let me implement this and test... looks like it should work? I'm looking at oauth2/internal/token.go at function 'newTokenRequest', and it seems it only adds the client_secret to the body of the request if it is not null/empty

naizerjohn-ms avatar Oct 14 '24 17:10 naizerjohn-ms

@naizerjohn-ms yep it adds it only if its defined, but you can define extra parameters with oauth2.SetAuthURLParam before that. Please test it if you have use-case :)

zetaab avatar Oct 14 '24 17:10 zetaab

we are also interested in this feature as well, waiting for the PR to be approve

francisconunesnavarro avatar Oct 23 '24 12:10 francisconunesnavarro

@zetaab This has been tested and verified. Will close issue now!

naizerjohn-ms avatar Nov 04 '24 17:11 naizerjohn-ms

Hello @zetaab! I am reopening this issue, but I may just create a new one for specificity. Your recommendation above as stated has been verified to work which is great! There is one issue though... the token refresh does not work when using the method above by setting the authURLparams. This is because when this package does a token refresh it calls the TokenSource method inside oauth2.go file here: image

TokenSource does NOT support passing in additional authURLparams like the Exchange method does. Any help to get this support implemented would be much appreciated! Or if I missed something, please show me!

naizerjohn-ms avatar Dec 30 '24 19:12 naizerjohn-ms

@naizerjohn-ms yeah well might be.. but the main issue is that none is reviewing. I have no idea how to get reviews

zetaab avatar Dec 30 '24 19:12 zetaab

Adding @rakyll @adg @bradfitz seeing that you all have made contributions recently!

naizerjohn-ms avatar Dec 30 '24 19:12 naizerjohn-ms

@cuishuang seeing that you have made a contribution recently, how may I be able to get someone to review/merge a PR?

naizerjohn-ms avatar Jan 21 '25 20:01 naizerjohn-ms

I'd love to be able to implement this!

michaeldcanady avatar Mar 14 '25 01:03 michaeldcanady

@naizerjohn-ms & co. I got reply finally:

This needs a https://go.dev/s/proposal

If someone do have time to continue with this feel free. I am not anymore working in job where I was working and this feature was needed.

zetaab avatar Apr 17 '25 04:04 zetaab

Thank you! Created Proposal #73431

michaeldcanady avatar Apr 18 '25 00:04 michaeldcanady

@cuishuang seeing that you have made a contribution recently, how may I be able to get someone to review/merge a PR?

ref: https://go.dev/doc/contribute

cuishuang avatar Apr 19 '25 01:04 cuishuang