2022-01-29 17:50:45 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
http "github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Provider represent 3rd party RelMeAuth provider.
|
|
|
|
type Provider struct {
|
|
|
|
Scopes []string
|
|
|
|
AuthURL string
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
Name string
|
|
|
|
Photo string
|
|
|
|
RedirectURL string
|
|
|
|
TokenURL string
|
|
|
|
UID string
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
|
2022-02-01 17:27:48 +00:00
|
|
|
//nolint: gochecknoglobals // structs cannot be contants
|
2022-01-29 17:50:45 +00:00
|
|
|
var (
|
2022-01-30 17:49:25 +00:00
|
|
|
ProviderDirect = Provider{
|
2022-02-01 17:27:48 +00:00
|
|
|
AuthURL: "/authorize",
|
|
|
|
ClientID: "",
|
|
|
|
ClientSecret: "",
|
|
|
|
Name: "IndieAuth",
|
|
|
|
Photo: path.Join("static", "icon.svg"),
|
|
|
|
RedirectURL: path.Join("callback"),
|
|
|
|
Scopes: []string{},
|
|
|
|
TokenURL: "/token",
|
|
|
|
UID: "direct",
|
|
|
|
URL: "/",
|
2022-01-29 17:50:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 17:49:25 +00:00
|
|
|
ProviderTwitter = Provider{
|
2022-02-01 17:27:48 +00:00
|
|
|
AuthURL: "https://twitter.com/i/oauth2/authorize",
|
|
|
|
ClientID: "",
|
|
|
|
ClientSecret: "",
|
|
|
|
Name: "Twitter",
|
|
|
|
Photo: path.Join("static", "providers", "twitter.svg"),
|
|
|
|
RedirectURL: path.Join("callback", "twitter"),
|
|
|
|
Scopes: []string{"tweet.read", "users.read"},
|
|
|
|
TokenURL: "https://api.twitter.com/2/oauth2/token",
|
|
|
|
UID: "twitter",
|
|
|
|
URL: "https://twitter.com/",
|
2022-01-29 17:50:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 17:49:25 +00:00
|
|
|
ProviderGitHub = Provider{
|
2022-02-01 17:27:48 +00:00
|
|
|
AuthURL: "https://github.com/login/oauth/authorize",
|
|
|
|
ClientID: "",
|
|
|
|
ClientSecret: "",
|
|
|
|
Name: "GitHub",
|
|
|
|
Photo: path.Join("static", "providers", "github.svg"),
|
|
|
|
RedirectURL: path.Join("callback", "github"),
|
|
|
|
Scopes: []string{"read:user", "user:email"},
|
|
|
|
TokenURL: "https://github.com/login/oauth/access_token",
|
|
|
|
UID: "github",
|
|
|
|
URL: "https://github.com/",
|
2022-01-29 17:50:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 17:49:25 +00:00
|
|
|
ProviderGitLab = Provider{
|
2022-02-01 17:27:48 +00:00
|
|
|
AuthURL: "https://gitlab.com/oauth/authorize",
|
|
|
|
ClientID: "",
|
|
|
|
ClientSecret: "",
|
|
|
|
Name: "GitLab",
|
|
|
|
Photo: path.Join("static", "providers", "gitlab.svg"),
|
|
|
|
RedirectURL: path.Join("callback", "gitlab"),
|
|
|
|
Scopes: []string{"read_user"},
|
|
|
|
TokenURL: "https://gitlab.com/oauth/token",
|
|
|
|
UID: "gitlab",
|
|
|
|
URL: "https://gitlab.com/",
|
2022-01-29 17:50:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 17:49:25 +00:00
|
|
|
ProviderMastodon = Provider{
|
2022-02-01 17:27:48 +00:00
|
|
|
AuthURL: "https://mstdn.io/oauth/authorize",
|
|
|
|
ClientID: "",
|
|
|
|
ClientSecret: "",
|
|
|
|
Name: "Mastodon",
|
|
|
|
Photo: path.Join("static", "providers", "mastodon.svg"),
|
|
|
|
RedirectURL: path.Join("callback", "mastodon"),
|
|
|
|
Scopes: []string{"read:accounts"},
|
|
|
|
TokenURL: "https://mstdn.io/oauth/token",
|
|
|
|
UID: "mastodon",
|
|
|
|
URL: "https://mstdn.io/",
|
2022-01-29 17:50:45 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// AuthCodeURL returns URL for authorize user in RelMeAuth client.
|
2022-01-30 17:49:25 +00:00
|
|
|
func (p Provider) AuthCodeURL(state string) string {
|
2022-02-01 17:27:48 +00:00
|
|
|
uri := http.AcquireURI()
|
|
|
|
defer http.ReleaseURI(uri)
|
|
|
|
uri.Update(p.AuthURL)
|
2022-01-29 17:50:45 +00:00
|
|
|
|
2022-02-01 17:27:48 +00:00
|
|
|
for key, val := range map[string]string{
|
2022-01-29 17:50:45 +00:00
|
|
|
"client_id": p.ClientID,
|
|
|
|
"redirect_uri": p.RedirectURL,
|
|
|
|
"response_type": "code",
|
|
|
|
"scope": strings.Join(p.Scopes, " "),
|
|
|
|
"state": state,
|
|
|
|
} {
|
2022-02-01 17:27:48 +00:00
|
|
|
uri.QueryArgs().Set(key, val)
|
2022-01-29 17:50:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 17:27:48 +00:00
|
|
|
return uri.String()
|
2022-01-29 17:50:45 +00:00
|
|
|
}
|