-
Notifications
You must be signed in to change notification settings - Fork 0
/
member_api.go
63 lines (55 loc) · 1.79 KB
/
member_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/diamondburned/arikawa/v3/discord"
)
// getDiscordAuthLink returns the Discord authentication link
// from the authentication server. This relies on having a symlink
// to the authentication server's artisan install.
func getDiscordAuthLink(user discord.User) string {
output, err := runGayauthCommand("generateDiscordAuthUrl", user)
if err != nil {
log.Fatalln(output, err)
}
return output
}
// isDiscordAuthenticated checks whether a user is authenticated
// in the database for Discord for a specific student type, and returns
// true if they are, or false otherwise. It also returns the student
// type that the user does have in its second return.
func isDiscordAuthenticated(user discord.User, studentType StudentType) (bool, string) {
output, err := runGayauthCommand("verifyDiscordAuth", user)
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 1 {
return false, ""
}
log.Fatalln(output, err)
}
for _, code := range studentType.Codes() {
if strings.EqualFold(code, output) {
return true, output
}
}
return false, output
}
// runGayauthCommand runs a command with the artisan console.
func runGayauthCommand(command string, user discord.User) (string, error) {
artisan := filepath.Join(os.Getenv("AUTH_ROOT"), "artisan") // gets path to Laravel Artisan
if _, err := os.Stat(artisan); err != nil {
return "", fmt.Errorf("AUTH_ROOT is not set correctly or artisan is missing")
}
generatorCommand := exec.Command(artisan, fmt.Sprintf("gayauth:%s", command), user.ID.String())
var out bytes.Buffer
generatorCommand.Stdout = &out
err := generatorCommand.Run()
if err != nil {
return "", err
}
return strings.TrimSpace(out.String()), nil
}