auth/internal/domain/provider.go

101 lines
2.5 KiB
Go
Raw Normal View History

2022-01-29 17:50:45 +00:00
package domain
import (
"net/url"
2022-01-29 17:50:45 +00:00
"path"
"strings"
)
// Provider represent 3rd party RelMeAuth provider.
type Provider struct {
AuthURL string
ClientID string
ClientSecret string
Name string
Photo string
RedirectURL string
TokenURL string
UID string
URL string
2023-07-06 23:11:53 +00:00
Scopes []string
2022-01-29 17:50:45 +00:00
}
2022-12-26 14:09:34 +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{
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
ProviderGitHub = Provider{
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{
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{
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 {
u, err := url.Parse(p.AuthURL)
if err != nil {
return ""
}
2022-01-29 17:50:45 +00:00
2023-07-06 23:11:53 +00:00
q := u.Query()
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,
} {
2023-07-06 23:11:53 +00:00
q.Set(key, val)
2022-01-29 17:50:45 +00:00
}
2023-07-06 23:11:53 +00:00
u.RawQuery = q.Encode()
return u.String()
2022-01-29 17:50:45 +00:00
}