oauth2 icon indicating copy to clipboard operation
oauth2 copied to clipboard

Custom grant types

Open j4k opened this issue 5 years ago • 3 comments

Hi, I would like the ability to extend and implement custom grant types.

My use case is to implement a 'social' grant type like the following lib: https://github.com/coderello/laravel-passport-social-grant

I'm currently porting an oauth implementation for an app into golang and need to emulate the above.

I don't think this is possible currently in go-oauth2, would you be open to it?

j4k avatar Apr 12 '19 10:04 j4k

there is an example: https://github.com/go2s/o2s/blob/master/http/server.go

captcha.EnableCaptchaAuth(svr, mcs, captcha.CaptchaLogSender)

wongoo avatar Apr 12 '19 10:04 wongoo

there is an example: https://github.com/go2s/o2s/blob/master/http/server.go

captcha.EnableCaptchaAuth(svr, mcs, captcha.CaptchaLogSender)

You linked to a different library. Is the suggestion here to implement a new server type that supports the custom grant?

casimcdaniels avatar Nov 06 '20 17:11 casimcdaniels

@casimcdaniels he means, that it's possible with wrappers, which redefine some methods

func (s *Oauth2Server) AddCustomerGrantType(grantType oauth2.GrantType, validator GrantTypeRequestValidator, handleConfigurer HandleConfigurer) {
	for _, t := range s.Config.AllowedGrantTypes {
		if t == grantType {
			panic("grant type already exist")
		}
	}

	s.Config.AllowedGrantTypes = append(s.Config.AllowedGrantTypes, grantType)
	customGrantRequestValidatorMap[grantType] = validator

	if handleConfigurer != nil {
		handleConfigurer(s.AddHandler)
	}
}
func (s *Oauth2Server) ValidationTokenRequest(r *http.Request) (gt oauth2.GrantType, tgr *oauth2.TokenGenerateRequest, err error) {
	gt = oauth2.GrantType(r.FormValue("grant_type"))

	if fn, ok := customGrantRequestValidatorMap[gt]; ok {
		gt, tgr, err = fn(r)
	} else {
		gt, tgr, err = s.Server.ValidationTokenRequest(r)
	}

	if err != nil {
		return
	}

	// ...
}

Link

ubombi avatar May 04 '22 09:05 ubombi