Skip to content

Return token#246

Open
djedjethai wants to merge 3 commits into
go-oauth2:masterfrom
djedjethai:returnToken
Open

Return token#246
djedjethai wants to merge 3 commits into
go-oauth2:masterfrom
djedjethai:returnToken

Conversation

@djedjethai
Copy link
Copy Markdown

Hi, I am using oauth2 as an authorization server to secure a microservices based app, doing like so I needed the *oauth2.Token to be returned as a payload(instead of being redirected), so I added 5 lines for(in /server/server.go).

type Server struct {
	Config                       *Config
	Manager                      oauth2.Manager
	..........
	IsModeAPI                    bool
}

func (s *Server) SetModeAPI() {
	s.IsModeAPI = true
}

func (s *Server) redirect(w http.ResponseWriter, req *AuthorizeRequest, data map[string]interface{}) error {
	if !s.IsModeAPI {
		uri, err := s.GetRedirectURI(req, data)
		if err != nil {
			return err
		}

		w.Header().Set("Location", uri)
		w.WriteHeader(302)
		return nil

	} else {
		w.Header().Set("Content-Type", "application/json")
		w.Header().Set("Cache-Control", "no-store")
		w.Header().Set("Pragma", "no-cache")

		w.WriteHeader(http.StatusOK)
		return json.NewEncoder(w).Encode(data)
	}
}

It does not create any breaking change, if you like it ?
As well I have given a simple but quite complet flow about a way to use it this way, in the /example/secureYourMicroservices, if you like it ? Wish you the best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants