This repository has been archived by the owner on Jul 19, 2022. It is now read-only.
-
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.
adding more integrations and features
- Loading branch information
Showing
40 changed files
with
2,906 additions
and
532 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
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,77 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/betorvs/sensubot/config" | ||
"github.com/betorvs/sensubot/usecase" | ||
oidc "github.com/coreos/go-oidc" | ||
"github.com/labstack/echo/v4" | ||
"google.golang.org/api/chat/v1" | ||
) | ||
|
||
const ( | ||
jwtURL string = "https://www.googleapis.com/service_accounts/v1/jwk/" | ||
chatIssuer string = "chat@system.gserviceaccount.com" | ||
) | ||
|
||
// GChatEvents func receive an post from GChat slash integration | ||
func GChatEvents(c echo.Context) error { | ||
// "Authorization" | ||
bearerToken := c.Request().Header.Get("Authorization") | ||
// fmt.Println(bearerToken) | ||
token := strings.Split(bearerToken, " ") | ||
if len(token) != 2 || !verifyJWT(config.Values.GoogleChatProjectID, token[1]) { | ||
// fmt.Println(len(token), verifyJWT(config.Values.GoogleChatProjectID, token[1])) | ||
return c.JSON(http.StatusForbidden, nil) | ||
} | ||
event := new(chat.DeprecatedEvent) | ||
if err := c.Bind(event); err != nil { | ||
return c.JSON(http.StatusBadRequest, err) | ||
} | ||
switch event.Type { | ||
case "ADDED_TO_SPACE": | ||
if event.Space.Type != "ROOM" { | ||
break | ||
} | ||
text := new(chat.Message) | ||
text.Text = "thanks for adding me." | ||
return c.JSON(http.StatusAccepted, text) | ||
case "MESSAGE": | ||
text := new(chat.Message) | ||
|
||
res, err := usecase.ParseGoogleChatCommand(event) | ||
if err != nil { | ||
return c.JSON(http.StatusBadRequest, err) | ||
} | ||
text.Text = fmt.Sprintf("you requested %s %s", event.Message.ArgumentText, res) | ||
return c.JSON(http.StatusAccepted, text) | ||
} | ||
return nil | ||
} | ||
|
||
func verifyJWT(audience, token string) bool { | ||
logLocal := config.GetLogger() | ||
logLocal.Debug("starting verifying") | ||
ctx := context.Background() | ||
keySet := oidc.NewRemoteKeySet(ctx, jwtURL+chatIssuer) | ||
|
||
configLocal := &oidc.Config{ | ||
SkipClientIDCheck: false, | ||
ClientID: audience, | ||
} | ||
newVerifier := oidc.NewVerifier(chatIssuer, keySet, configLocal) | ||
test, err := newVerifier.Verify(ctx, token) | ||
if err != nil { | ||
logLocal.Debug("Audience dont match") | ||
return false | ||
} | ||
if len(test.Audience) == 1 { | ||
logLocal.Debug("Audience matches") | ||
} | ||
|
||
return true | ||
} |
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,24 @@ | ||
package controller | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPostGChatEvents(t *testing.T) { | ||
// Setup | ||
e := echo.New() | ||
req := httptest.NewRequest(http.MethodPost, "/", nil) | ||
rec := httptest.NewRecorder() | ||
c := e.NewContext(req, rec) | ||
c.SetPath("/sensubot/v1/gchat") | ||
|
||
// Assertions | ||
if assert.NoError(t, GChatEvents(c)) { | ||
assert.Equal(t, http.StatusForbidden, rec.Code) | ||
} | ||
} |
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,15 @@ | ||
package domain | ||
|
||
import "github.com/betorvs/sensubot/appcontext" | ||
|
||
// AlertManagerRepository interface | ||
type AlertManagerRepository interface { | ||
appcontext.Component | ||
// GetAlertManager receives an string URL and return []byte and error | ||
GetAlertManager(amURL string) (result []byte, err error) | ||
} | ||
|
||
// GetAlertManagerRepository func return AlertManagerRepository interface | ||
func GetAlertManagerRepository() AlertManagerRepository { | ||
return appcontext.Current.Get(appcontext.AlertManagerRepository).(AlertManagerRepository) | ||
} |
Oops, something went wrong.