Skip to content
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

Refactored the code and added flow manager for Oauth setup #125

Open
wants to merge 7 commits into
base: oauth_epic
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ require (
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rudderlabs/analytics-go v3.3.3+incompatible // indirect
github.com/segmentio/backo-go v1.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tinylib/msgp v1.2.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/wiggin77/merror v1.0.5 // indirect
github.com/wiggin77/srslog v1.0.1 // indirect
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7q
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rudderlabs/analytics-go v3.3.3+incompatible h1:OG0XlKoXfr539e2t1dXtTB+Gr89uFW+OUNQBVhHIIBY=
github.com/rudderlabs/analytics-go v3.3.3+incompatible/go.mod h1:LF8/ty9kUX4PTY3l5c97K3nZZaX5Hwsvt+NBaRL/f30=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/segmentio/backo-go v1.1.0 h1:cJIfHQUdmLsd8t9IXqf5J8SdrOMn9vMa7cIvOavHAhc=
github.com/segmentio/backo-go v1.1.0/go.mod h1:ckenwdf+v/qbyhVdNPWHnqh2YdJBED1O9cidYyM5J18=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY=
github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM=
Expand Down Expand Up @@ -185,6 +189,13 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tinylib/msgp v1.2.0 h1:0uKB/662twsVBpYUPbokj4sTSKhWFKB7LopO2kWK8lY=
github.com/tinylib/msgp v1.2.0/go.mod h1:2vIGs3lcUo8izAATNobrCHevYZC/LMsJtw4JPiYPHro=
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
Expand All @@ -197,6 +208,8 @@ github.com/wiggin77/merror v1.0.5 h1:P+lzicsn4vPMycAf2mFf7Zk6G9eco5N+jB1qJ2XW3ME
github.com/wiggin77/merror v1.0.5/go.mod h1:H2ETSu7/bPE0Ymf4bEwdUoo73OOEkdClnoRisfw0Nm0=
github.com/wiggin77/srslog v1.0.1 h1:gA2XjSMy3DrRdX9UqLuDtuVAAshb8bE1NhX1YK0Qe+8=
github.com/wiggin77/srslog v1.0.1/go.mod h1:fehkyYDq1QfuYn60TDPu9YdY2bB85VUW2mvN1WynEls=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c h1:3lbZUMbMiGUW/LMkfsEABsc5zNT9+b1CvsJx47JzJ8g=
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63MEE3IF9l2/ebyx59GyGgPi+tICQdmM=
go.opencensus.io v0.18.0/go.mod h1:vKdFvxhtzZ9onBp9VKHK8z/sRpBMnKAsufL7wlDrCOA=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package main

import (
"html/template"
Expand Down
49 changes: 22 additions & 27 deletions server/command/command.go → server/command.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package main

import (
"fmt"
Expand All @@ -18,7 +18,7 @@ type PluginAPI interface {
GetBundlePath() (string, error)
}

type HandlerFunc func(context *model.CommandArgs, args ...string) *model.CommandResponse
type HandlerFunc func(p *Plugin, context *model.CommandArgs, args ...string) *model.CommandResponse

type Handler struct {
handlers map[string]HandlerFunc
Expand Down Expand Up @@ -46,16 +46,6 @@ const (
)

const (
installServerHelp = `
To configure the plugin, create a new app in your Confluence Server following these steps:
1. Navigate to **Settings > Apps > Manage Apps**. For older versions of Confluence, navigate to **Administration > Applications > Add-ons > Manage add-ons**.
2. Choose **Settings** at the bottom of the page, enable development mode, and apply the change. Development mode allows you to install apps from outside of the Atlassian Marketplace.
3. Press **Upload app**.
4. Choose **From my computer** and upload the Mattermost for Confluence OBR file.
5. Once the app is installed, press **Configure** to open the configuration page.
6. In the **Webhook URL** field, enter: %s
7. Press **Save** to finish the setup.
`
installCloudHelp = `
To finish the configuration, add a new app in your Confluence Cloud instance following these steps:
1. Navigate to **Settings > Apps > Manage Apps**.
Expand Down Expand Up @@ -128,9 +118,9 @@ func getAutoCompleteData() *model.AutocompleteData {
return confluence
}

func executeConfluenceDefault(context *model.CommandArgs, args ...string) *model.CommandResponse {
func executeConfluenceDefault(p *Plugin, context *model.CommandArgs, args ...string) *model.CommandResponse {
out := invalidCommand + "\n\n"
out += getFullHelpText(context, args...)
out += getFullHelpText(p, context, args...)

return &model.CommandResponse{
ResponseType: model.CommandResponseTypeEphemeral,
Expand All @@ -147,25 +137,25 @@ func postCommandResponse(context *model.CommandArgs, text string) {
_ = config.Mattermost.SendEphemeralPost(context.UserId, post)
}

func (ch Handler) Handle(context *model.CommandArgs, args ...string) *model.CommandResponse {
func (ch Handler) Handle(p *Plugin, context *model.CommandArgs, args ...string) *model.CommandResponse {
if !util.IsSystemAdmin(context.UserId) {
postCommandResponse(context, commandsOnlySystemAdmin)
return &model.CommandResponse{}
}

if len(args) == 0 {
return ch.handlers["help"](context, "")
return ch.handlers["help"](p, context, "")
}
for n := len(args); n > 0; n-- {
h := ch.handlers[strings.Join(args[:n], "/")]
if h != nil {
return h(context, args[n:]...)
return h(p, context, args[n:]...)
}
}
return ch.defaultHandler(context, args...)
return ch.defaultHandler(p, context, args...)
}

func showInstallCloudHelp(context *model.CommandArgs, args ...string) *model.CommandResponse {
func showInstallCloudHelp(p *Plugin, context *model.CommandArgs, args ...string) *model.CommandResponse {
if !util.IsSystemAdmin(context.UserId) {
postCommandResponse(context, installOnlySystemAdmin)
return &model.CommandResponse{}
Expand All @@ -176,18 +166,23 @@ func showInstallCloudHelp(context *model.CommandArgs, args ...string) *model.Com
return &model.CommandResponse{}
}

func showInstallServerHelp(context *model.CommandArgs, args ...string) *model.CommandResponse {
func showInstallServerHelp(p *Plugin, context *model.CommandArgs, args ...string) *model.CommandResponse {
if !util.IsSystemAdmin(context.UserId) {
postCommandResponse(context, installOnlySystemAdmin)
return &model.CommandResponse{}
}

serverURL := util.GetPluginURL() + util.GetConfluenceServerWebhookURLPath()
postCommandResponse(context, fmt.Sprintf(installServerHelp, serverURL))
err := p.flowManager.StartSetupWizard(context.UserId, "")
if err != nil {
return &model.CommandResponse{}
}

postCommandResponse(context, "Please continue with confluence bot DM for the setup.")

return &model.CommandResponse{}
}

func deleteSubscription(context *model.CommandArgs, args ...string) *model.CommandResponse {
func deleteSubscription(p *Plugin, context *model.CommandArgs, args ...string) *model.CommandResponse {
if len(args) == 0 {
postCommandResponse(context, specifyAlias)
return &model.CommandResponse{}
Expand All @@ -201,7 +196,7 @@ func deleteSubscription(context *model.CommandArgs, args ...string) *model.Comma
return &model.CommandResponse{}
}

func listChannelSubscription(context *model.CommandArgs, args ...string) *model.CommandResponse {
func listChannelSubscription(p *Plugin, context *model.CommandArgs, args ...string) *model.CommandResponse {
channelSubscriptions, gErr := service.GetSubscriptionsByChannelID(context.ChannelId)
if gErr != nil {
postCommandResponse(context, gErr.Error())
Expand All @@ -217,14 +212,14 @@ func listChannelSubscription(context *model.CommandArgs, args ...string) *model.
return &model.CommandResponse{}
}

func confluenceHelpCommand(context *model.CommandArgs, args ...string) *model.CommandResponse {
helpText := getFullHelpText(context, args...)
func confluenceHelpCommand(p *Plugin, context *model.CommandArgs, args ...string) *model.CommandResponse {
helpText := getFullHelpText(p, context, args...)

postCommandResponse(context, helpText)
return &model.CommandResponse{}
}

func getFullHelpText(context *model.CommandArgs, args ...string) string {
func getFullHelpText(_ *Plugin, context *model.CommandArgs, _ ...string) string {
helpText := commonHelpText
if util.IsSystemAdmin(context.UserId) {
helpText += sysAdminHelpText
Expand Down
32 changes: 31 additions & 1 deletion server/config/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"encoding/json"
"strings"

"github.com/mattermost/mattermost/server/public/plugin"
Expand All @@ -19,7 +20,10 @@ var (
)

type Configuration struct {
Secret string `json:"Secret"`
Secret string `json:"Secret"`
ConfluenceOAuthClientID string
ConfluenceOAuthClientSecret string
ConfluenceURL string
}

func GetConfig() *Configuration {
Expand All @@ -43,3 +47,29 @@ func (c *Configuration) IsValid() error {

return nil
}

func (c *Configuration) Sanitize() {
// Ensure Confluence ends with a slash
c.ConfluenceURL = strings.TrimRight(c.ConfluenceURL, "/")

c.ConfluenceOAuthClientID = strings.TrimSpace(c.ConfluenceOAuthClientID)
c.ConfluenceOAuthClientSecret = strings.TrimSpace(c.ConfluenceOAuthClientSecret)
}

func (c *Configuration) IsOAuthConfigured() bool {
return (c.ConfluenceOAuthClientID != "" && c.ConfluenceOAuthClientSecret != "")
}

func (c *Configuration) ToMap() (map[string]interface{}, error) {
var out map[string]interface{}
data, err := json.Marshal(c)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &out)
if err != nil {
return nil, err
}

return out, nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package main

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package main

import (
"net/http"
Expand Down
4 changes: 2 additions & 2 deletions server/controller/main.go → server/controller.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package main

import (
"crypto/subtle"
Expand Down Expand Up @@ -41,7 +41,7 @@ func getEndpointKey(endpoint *Endpoint) string {
}

// InitAPI initializes the REST API
func InitAPI() *mux.Router {
func (p *Plugin) InitAPI() *mux.Router {
r := mux.NewRouter()
handleStaticFiles(r)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package main

import (
"net/http"
Expand Down
Loading
Loading