2020-03-07 14:06:57 +03:00
|
|
|
package oauth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-03-09 13:21:49 +03:00
|
|
|
"net/url"
|
2020-03-07 14:06:57 +03:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/v2/auth/provider"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewProvider returns an initialised oauth provider
|
|
|
|
func NewProvider(opts ...provider.Option) provider.Provider {
|
|
|
|
var options provider.Options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return &oauth{options}
|
|
|
|
}
|
|
|
|
|
|
|
|
type oauth struct {
|
|
|
|
opts provider.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *oauth) String() string {
|
|
|
|
return "oauth"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *oauth) Options() provider.Options {
|
|
|
|
return o.opts
|
|
|
|
}
|
|
|
|
|
2020-04-01 17:36:22 +03:00
|
|
|
func (o *oauth) Endpoint(opts ...provider.EndpointOption) string {
|
|
|
|
var options provider.EndpointOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:11:35 +03:00
|
|
|
params := make(url.Values)
|
|
|
|
params.Add("response_type", "code")
|
|
|
|
|
2020-04-01 19:11:46 +03:00
|
|
|
if len(options.State) > 0 {
|
|
|
|
params.Add("state", options.State)
|
2020-04-01 17:36:22 +03:00
|
|
|
}
|
|
|
|
|
2020-03-12 16:11:35 +03:00
|
|
|
if clientID := o.opts.ClientID; len(clientID) > 0 {
|
|
|
|
params.Add("client_id", clientID)
|
|
|
|
}
|
2020-03-07 14:06:57 +03:00
|
|
|
|
|
|
|
if scope := o.opts.Scope; len(scope) > 0 {
|
2020-04-01 17:36:22 +03:00
|
|
|
params.Add("scope", scope)
|
2020-03-07 14:06:57 +03:00
|
|
|
}
|
|
|
|
|
2020-03-12 16:11:35 +03:00
|
|
|
if redir := o.Redirect(); len(redir) > 0 {
|
2020-03-09 13:21:49 +03:00
|
|
|
params.Add("redirect_uri", redir)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v?%v", o.opts.Endpoint, params.Encode())
|
2020-03-07 14:06:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *oauth) Redirect() string {
|
|
|
|
return o.opts.Redirect
|
|
|
|
}
|