-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
786b991
commit 9e7267a
Showing
6 changed files
with
161 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
|
||
discord "github.com/bwmarrin/discordgo" | ||
"github.com/joho/godotenv" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
var clientID, clientSecret, redirectURI string | ||
|
||
func init() { | ||
err := godotenv.Load() | ||
if err != nil { | ||
log.Fatal("Error loading .env file") | ||
} | ||
clientID = os.Getenv("DISCORD_ID") | ||
clientSecret = os.Getenv("DISCORD_SECRET") | ||
redirectURI = "http://localhost:8001/oauth/discord" | ||
} | ||
|
||
func ExchangeCodeForToken(code string) (*oauth2.Token, error) { | ||
form := url.Values{} | ||
form.Add("client_id", clientID) | ||
form.Add("client_secret", clientSecret) | ||
form.Add("grant_type", "authorization_code") | ||
form.Add("code", code) | ||
form.Add("redirect_uri", redirectURI) | ||
|
||
resp, err := http.PostForm("https://discordapp.com/api/oauth2/token", form) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var token oauth2.Token | ||
err = json.Unmarshal(body, &token) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &token, nil | ||
} | ||
|
||
func FetchUser(token *oauth2.Token) (*discord.User, error) { | ||
req, err := http.NewRequest("GET", "https://discordapp.com/api/users/@me", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Authorization", "Bearer "+token.AccessToken) | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var user discord.User | ||
err = json.Unmarshal(body, &user) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &user, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
) | ||
|
||
func JwtDecode(jwtToken string) (string, error) { | ||
// 解析JWT | ||
parsedToken, err := jwt.Parse(jwtToken, func(token *jwt.Token) (interface{}, error) { | ||
// 验证算法和密钥 | ||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { | ||
return nil, fmt.Errorf("invalid signing method") | ||
} | ||
return []byte("my_secret_key"), nil | ||
}) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
claims, ok := parsedToken.Claims.(jwt.MapClaims) | ||
if !ok || !parsedToken.Valid { | ||
return "", err | ||
} | ||
address := claims["address"].(string) | ||
return address, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters