-
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
Urban Ishimwe
committed
Apr 27, 2020
1 parent
2d8bfc2
commit 660965e
Showing
22 changed files
with
168 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ PUBLIC_KEY= | |
|
||
# slack keys | ||
SLACK_CLIENT_ID= | ||
SLACK_CLIENT_SECRET= | ||
SLACK_CLIENT_SECRET= | ||
SLACK_SIGNING_SECRET= |
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,19 @@ | ||
package config_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/rwandaopensource/botx/config" | ||
"github.com/rwandaopensource/botx/helper" | ||
) | ||
|
||
func TestConfigParseError(t *testing.T) { | ||
tmp := os.Getenv("PRIVATE_KEY") | ||
os.Setenv("PRIVATE_KEY", "p") | ||
config.Config(false) | ||
if v := os.Getenv("PRIVATE_KEY"); v != "p" { | ||
helper.TestError(t, "parsing config failed") | ||
} | ||
os.Setenv("PRIVATE_KEY", tmp) | ||
} |
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,48 @@ | ||
package controller | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/rwandaopensource/botx/database" | ||
"github.com/rwandaopensource/botx/helper" | ||
"github.com/slack-go/slack" | ||
) | ||
|
||
var ( | ||
slackSigningSecret = os.Getenv("SLACK_SIGNING_SECRET") | ||
slackClientID = os.Getenv("SLACK_CLIENT_ID") | ||
slackClientSecret = os.Getenv("SLACK_CLIENT_SECRET") | ||
slackError = "invalid_client_id,bad_client_secret,oauth_authorization_url_mismatch,bad_redirect_uri" | ||
) | ||
|
||
// Install http.handler for installer command | ||
func Install(rw http.ResponseWriter, r *http.Request) { | ||
defer r.Body.Close() | ||
code := r.URL.Query().Get("code") | ||
e := r.URL.Query().Get("error") | ||
if e != "" { | ||
if strings.Contains(slackError, e) { | ||
rw.WriteHeader(http.StatusInternalServerError) | ||
helper.PrintError(errors.New(e), "during install") | ||
return | ||
} | ||
rw.WriteHeader(http.StatusInternalServerError) | ||
fmt.Fprint(rw, strings.Join(strings.Split(e, "_"), " ")) | ||
return | ||
} | ||
t, err := slack.GetOAuthResponse(http.DefaultClient, slackClientID, slackClientSecret, code, "") | ||
if err != nil { | ||
rw.WriteHeader(http.StatusNotAcceptable) | ||
return | ||
} | ||
team := database.Workspace{*t, time.Now().Nanosecond(), time.Now().Nanosecond()} | ||
err = database.CreateWorkspace(team) | ||
if err != nil { | ||
rw.WriteHeader(http.StatusInternalServerError) | ||
} | ||
} |
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,23 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/slack-go/slack" | ||
) | ||
|
||
// Workspace represent workspace | ||
type Workspace struct { | ||
slack.OAuthResponse | ||
CreatedAt int `json:"created_at"` | ||
UpdatedAt int `json:"updated_at"` | ||
} | ||
|
||
// CreateWorkspace store workspace details in the database | ||
func CreateWorkspace(workspace Workspace) error { | ||
_, err := WorkspaceModel.InsertOne(context.TODO(), workspace, nil) | ||
if err != nil { | ||
return err | ||
} | ||
return 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,23 @@ | ||
package helper | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// InstallHTML return html for installing app | ||
func InstallHTML() string { | ||
slackClientID := os.Getenv("SLACK_CLIENT_ID") | ||
host := os.Getenv("REDIRECT_URI") | ||
return fmt.Sprintf(` | ||
<html lang="en"> | ||
<head> | ||
<title> Install slack </tile> | ||
</head> | ||
<h1> Install this app in your slack workspace </h1> | ||
<a href="https://slack.com/oauth/v2/authorize?client_id=%s&scope=im:write,chat:write,users:read,reactions:read,chat:write.public,incoming-webhook&redirect_uri=%s"> | ||
<img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/[email protected] 2x"> | ||
</a> | ||
</html> | ||
`, slackClientID, host) | ||
} |
File renamed without changes.
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
File renamed without changes.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.