go-oauth2 / oauth2

OAuth 2.0 server library for the Go programming language.
https://pkg.go.dev/github.com/go-oauth2/oauth2/v4
MIT License
3.31k stars 563 forks source link

Return token #246

Open djedjethai opened 1 year ago

djedjethai commented 1 year ago

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.