added oauth flow to get authorization code
This commit is contained in:
39
internal/discord/oauth.go
Normal file
39
internal/discord/oauth.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package discord
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Token struct {
|
||||
AccessToken string
|
||||
TokenType string
|
||||
ExpiresIn int
|
||||
RefreshToken string
|
||||
Scope string
|
||||
}
|
||||
|
||||
const oauthurl string = "https://discord.com/oauth2/authorize"
|
||||
|
||||
func GetOAuthLink(cfg *Config, state string, trustedHost string) (string, error) {
|
||||
if cfg == nil {
|
||||
return "", errors.New("cfg cannot be nil")
|
||||
}
|
||||
if state == "" {
|
||||
return "", errors.New("state cannot be empty")
|
||||
}
|
||||
if trustedHost == "" {
|
||||
return "", errors.New("trustedHost cannot be empty")
|
||||
}
|
||||
values := url.Values{}
|
||||
values.Add("response_type", "code")
|
||||
values.Add("client_id", cfg.ClientID)
|
||||
values.Add("scope", cfg.OAuthScopes)
|
||||
values.Add("state", state)
|
||||
values.Add("redirect_uri", fmt.Sprintf("%s/%s", trustedHost, cfg.RedirectPath))
|
||||
values.Add("prompt", "none")
|
||||
|
||||
return fmt.Sprintf("%s?%s", oauthurl, values.Encode()), nil
|
||||
}
|
||||
Reference in New Issue
Block a user