-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow users to send access request messages via Discord #11
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package discord | ||
|
||
// ----------- // | ||
// Channel API // | ||
// ----------- // | ||
|
||
// https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure | ||
type EmbedAuthor struct { | ||
Name string `json:"name"` | ||
IconURL string `json:"icon_url"` | ||
} | ||
|
||
// https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure | ||
type EmbedField struct { | ||
Name string `json:"name"` | ||
Value string `json:"value"` | ||
Inline bool `json:"inline"` | ||
} | ||
|
||
// https://discord.com/developers/docs/resources/channel#embed-object | ||
type Embed struct { | ||
Title string `json:"title"` | ||
Color int `json:"color"` | ||
Author EmbedAuthor `json:"author"` | ||
Fields []EmbedField `json:"fields"` | ||
} | ||
|
||
// ----------- // | ||
// Webhook API // | ||
// ----------- // | ||
|
||
// https://discord.com/developers/docs/resources/webhook#execute-webhook | ||
type WebhookMessage struct { | ||
Username string `json:"username"` | ||
AvatarURL string `json:"avatar_url"` | ||
Embeds []Embed `json:"embeds"` | ||
} |
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,87 @@ | ||
package web | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"go.mkw.re/ghidra-panel/common" | ||
"go.mkw.re/ghidra-panel/discord" | ||
) | ||
|
||
func (s *Server) handleRequestAccess(wr http.ResponseWriter, req *http.Request) { | ||
if req.Method != http.MethodPost { | ||
http.Error(wr, "Method not allowed", http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
ident, ok := s.checkAuth(req) | ||
if !ok { | ||
http.Error(wr, "Not authorized", http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
if err := req.ParseForm(); err != nil { | ||
http.Error(wr, "Bad request", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// TODO query ACL to ensure user account exists | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this TODO comment ... If the username doesn't exist in the UserManager, we can just add them before authorizing. |
||
|
||
message := s.writeMessage(ident) | ||
|
||
// Send access request message | ||
ctx := context.TODO() | ||
payloadBuf, _ := json.Marshal(&message) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.Config.DiscordWebhookURL, bytes.NewReader(payloadBuf)) | ||
if err != nil { | ||
http.Error(wr, "Internal server error", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
req.Header.Set("content-type", "application/json") | ||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
// TODO properly handle | ||
http.Redirect(wr, req, "/?access_request=failure", http.StatusTemporaryRedirect) | ||
return | ||
} | ||
defer res.Body.Close() | ||
|
||
http.Redirect(wr, req, "/?access_request=success", http.StatusTemporaryRedirect) | ||
} | ||
|
||
func (s *Server) writeMessage(ident *common.Identity) discord.WebhookMessage { | ||
embedAuthor := discord.EmbedAuthor{ | ||
Name: ident.Username, | ||
IconURL: fmt.Sprintf("https://cdn.discordapp.com/avatars/%d/%s.png", ident.ID, ident.AvatarHash), | ||
} | ||
|
||
hostnameField := discord.EmbedField{ | ||
Name: "Hostname", | ||
Value: s.Config.GhidraEndpoint.Hostname, | ||
Inline: true, | ||
} | ||
|
||
portField := discord.EmbedField{ | ||
Name: "Port", | ||
Value: strconv.FormatUint(uint64(s.Config.GhidraEndpoint.Port), 10), | ||
Inline: true, | ||
} | ||
|
||
ghidraEmbed := discord.Embed{ | ||
Title: fmt.Sprintf("%s has requested access to the following Ghidra server:", ident.Username), | ||
Color: 0x77DD77, | ||
Author: embedAuthor, | ||
Fields: []discord.EmbedField{hostnameField, portField}, | ||
} | ||
|
||
return discord.WebhookMessage{ | ||
Username: "Panel", | ||
AvatarURL: "", | ||
Embeds: []discord.Embed{ghidraEmbed}, | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine for now. I suggest putting user information in the SQLite database long term, so the JWT doesn't grow without bounds.