added oauth flow to get authorization code
This commit is contained in:
50
internal/discord/config.go
Normal file
50
internal/discord/config.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package discord
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.haelnorr.com/h/golib/env"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ClientID string // ENV DISCORD_CLIENT_ID: Discord application client ID (required)
|
||||
ClientSecret string // ENV DISCORD_CLIENT_SECRET: Discord application client secret (required)
|
||||
OAuthScopes string // Authorisation scopes for OAuth
|
||||
RedirectPath string // ENV DISCORD_REDIRECT_PATH: Path for the OAuth redirect handler (required)
|
||||
}
|
||||
|
||||
func ConfigFromEnv() (any, error) {
|
||||
cfg := &Config{
|
||||
ClientID: env.String("DISCORD_CLIENT_ID", ""),
|
||||
ClientSecret: env.String("DISCORD_CLIENT_SECRET", ""),
|
||||
OAuthScopes: getOAuthScopes(),
|
||||
RedirectPath: env.String("DISCORD_REDIRECT_PATH", ""),
|
||||
}
|
||||
|
||||
// Check required fields
|
||||
if cfg.ClientID == "" {
|
||||
return nil, errors.New("Envar not set: DISCORD_CLIENT_ID")
|
||||
}
|
||||
if cfg.ClientSecret == "" {
|
||||
return nil, errors.New("Envar not set: DISCORD_CLIENT_SECRET")
|
||||
}
|
||||
if cfg.RedirectPath == "" {
|
||||
return nil, errors.New("Envar not set: DISCORD_REDIRECT_PATH")
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func getOAuthScopes() string {
|
||||
list := []string{
|
||||
"connections",
|
||||
"email",
|
||||
"guilds",
|
||||
"gdm.join",
|
||||
"guilds.members.read",
|
||||
"identify",
|
||||
}
|
||||
scopes := strings.Join(list, "+")
|
||||
return scopes
|
||||
}
|
||||
Reference in New Issue
Block a user