|
package mockslack |
|
|
|
import ( |
|
"encoding/json" |
|
"log" |
|
"net/http" |
|
"strings" |
|
|
|
"github.com/davecgh/go-spew/spew" |
|
"github.com/pkg/errors" |
|
) |
|
|
|
|
|
type Server struct { |
|
*state |
|
|
|
mux *http.ServeMux |
|
|
|
handler http.Handler |
|
|
|
urlPrefix string |
|
} |
|
|
|
|
|
func NewServer() *Server { |
|
srv := &Server{ |
|
mux: http.NewServeMux(), |
|
state: newState(), |
|
} |
|
|
|
srv.mux.HandleFunc("/actions/response", srv.ServeActionResponse) |
|
srv.mux.HandleFunc("/api/chat.postMessage", srv.ServeChatPostMessage) |
|
srv.mux.HandleFunc("/api/chat.postEphemeral", srv.ServeChatPostMessage) |
|
srv.mux.HandleFunc("/api/chat.update", srv.ServeChatUpdate) |
|
srv.mux.HandleFunc("/api/conversations.info", srv.ServeConversationsInfo) |
|
srv.mux.HandleFunc("/api/conversations.list", srv.ServeConversationsList) |
|
srv.mux.HandleFunc("/api/users.conversations", srv.ServeConversationsList) |
|
srv.mux.HandleFunc("/api/users.info", srv.ServeUsersInfo) |
|
srv.mux.HandleFunc("/api/oauth.access", srv.ServeOAuthAccess) |
|
srv.mux.HandleFunc("/api/auth.revoke", srv.ServeAuthRevoke) |
|
srv.mux.HandleFunc("/api/auth.test", srv.ServeAuthTest) |
|
srv.mux.HandleFunc("/api/channels.create", srv.ServeChannelsCreate) |
|
srv.mux.HandleFunc("/api/groups.create", srv.ServeGroupsCreate) |
|
srv.mux.HandleFunc("/api/team.info", srv.ServeTeamInfo) |
|
srv.mux.HandleFunc("/api/usergroups.list", srv.ServeUserGroupList) |
|
srv.mux.HandleFunc("/api/usergroups.users.update", srv.ServeUserGroupsUsersUpdate) |
|
|
|
|
|
srv.mux.HandleFunc("/stats", func(w http.ResponseWriter, req *http.Request) { |
|
srv.mx.Lock() |
|
defer srv.mx.Unlock() |
|
spew.Fdump(w) |
|
}) |
|
|
|
|
|
srv.mux.HandleFunc("/api/", func(w http.ResponseWriter, req *http.Request) { |
|
err := json.NewEncoder(w).Encode(response{Err: "unknown_method: " + strings.TrimPrefix(req.URL.Path, "/api/")}) |
|
if err != nil { |
|
log.Println("ERROR:", err) |
|
} |
|
}) |
|
|
|
srv.mux.HandleFunc("/state", func(w http.ResponseWriter, req *http.Request) { |
|
srv.mx.Lock() |
|
defer srv.mx.Unlock() |
|
spew.Fdump(w, srv.state) |
|
}) |
|
|
|
srv.handler = middleware(srv.mux, |
|
srv.tokenMiddleware, |
|
srv.loginMiddleware, |
|
) |
|
|
|
return srv |
|
} |
|
|
|
|
|
func (s *Server) SetURLPrefix(prefix string) { |
|
s.urlPrefix = prefix |
|
} |
|
|
|
|
|
const TokenCookieName = "slack_token" |
|
|
|
|
|
type AppInfo struct { |
|
Name string |
|
ClientID string |
|
ClientSecret string |
|
AccessToken string |
|
TeamID string |
|
|
|
SigningSecret string |
|
|
|
ActionURL string |
|
} |
|
|
|
func (s *Server) SetActionURL(appID string, actionURL string) { |
|
s.mx.Lock() |
|
defer s.mx.Unlock() |
|
s.apps[appID].ActionURL = actionURL |
|
} |
|
|
|
|
|
func (st *state) InstallStaticApp(app AppInfo, scopes ...string) (*AppInfo, error) { |
|
st.mx.Lock() |
|
defer st.mx.Unlock() |
|
|
|
if app.ClientID == "" { |
|
app.ClientID = st.gen.ClientID() |
|
} |
|
if app.ClientSecret == "" { |
|
app.ClientSecret = st.gen.ClientSecret() |
|
} |
|
if app.AccessToken == "" { |
|
app.AccessToken = st.gen.UserAccessToken() |
|
} |
|
app.TeamID = st.teamID |
|
|
|
if !clientIDRx.MatchString(app.ClientID) { |
|
return nil, errors.Errorf("invalid client ID format: %s", app.ClientID) |
|
} |
|
if !clientSecretRx.MatchString(app.ClientSecret) { |
|
return nil, errors.Errorf("invalid client secret format: %s", app.ClientSecret) |
|
} |
|
if !userAccessTokenRx.MatchString(app.AccessToken) { |
|
return nil, errors.Errorf("invalid access token format: %s", app.AccessToken) |
|
} |
|
|
|
for _, scope := range scopes { |
|
if !scopeRx.MatchString(scope) { |
|
panic("invalid scope format: " + scope) |
|
} |
|
} |
|
|
|
tok := &AuthToken{ |
|
ID: app.AccessToken, |
|
Scopes: scopes, |
|
User: app.ClientID, |
|
} |
|
|
|
st.tokens[tok.ID] = tok |
|
st.apps[tok.User] = &appState{ |
|
App: App{ |
|
ID: app.ClientID, |
|
Name: app.Name, |
|
Secret: app.ClientSecret, |
|
AuthToken: tok, |
|
ActionURL: app.ActionURL, |
|
|
|
SigningSecret: app.SigningSecret, |
|
}, |
|
} |
|
|
|
return &app, nil |
|
} |
|
|
|
|
|
func (st *state) InstallApp(name string, scopes ...string) AppInfo { |
|
app, err := st.InstallStaticApp(AppInfo{Name: name}, scopes...) |
|
if err != nil { |
|
|
|
panic(err) |
|
} |
|
return *app |
|
} |
|
|
|
|
|
type UserInfo struct { |
|
ID string |
|
Name string |
|
AuthToken string |
|
} |
|
|
|
|
|
func (st *state) NewUser(name string) UserInfo { |
|
usr := st.newUser(User{Name: name}) |
|
tok := st.newToken(AuthToken{ |
|
User: usr.ID, |
|
Scopes: []string{"user"}, |
|
}) |
|
|
|
return UserInfo{ |
|
ID: usr.ID, |
|
Name: usr.Name, |
|
AuthToken: tok.ID, |
|
} |
|
} |
|
|
|
|
|
type ChannelInfo struct { |
|
ID, Name string |
|
} |
|
|
|
|
|
func (st *state) NewChannel(name string) ChannelInfo { |
|
info := ChannelInfo{ |
|
ID: st.gen.ChannelID(), |
|
Name: name, |
|
} |
|
|
|
st.mx.Lock() |
|
st.channels[info.ID] = &channelState{Channel: Channel{ |
|
ID: info.ID, |
|
Name: info.Name, |
|
IsChannel: true, |
|
}} |
|
st.mx.Unlock() |
|
|
|
return info |
|
} |
|
|
|
|
|
type UserGroupInfo struct { |
|
ID, Name, Handle string |
|
} |
|
|
|
|
|
func (st *state) NewUserGroup(name string) UserGroupInfo { |
|
info := UserGroupInfo{ |
|
ID: st.gen.UserGroupID(), |
|
Name: name, |
|
Handle: name, |
|
} |
|
|
|
st.mx.Lock() |
|
st.usergroups[info.ID] = &usergroupState{UserGroup: UserGroup{ |
|
ID: info.ID, |
|
Name: info.Name, |
|
Handle: info.Handle, |
|
IsUserGroup: true, |
|
}} |
|
st.mx.Unlock() |
|
|
|
return info |
|
} |
|
|
|
|
|
func (st *state) UserGroupUserIDs(ugID string) []string { |
|
st.mx.Lock() |
|
defer st.mx.Unlock() |
|
|
|
ug := st.usergroups[ugID] |
|
if ug == nil { |
|
return nil |
|
} |
|
|
|
users := make([]string, len(ug.Users)) |
|
copy(users, ug.Users) |
|
|
|
return users |
|
} |
|
|
|
|
|
func (st *state) Messages(chanID string) []Message { |
|
st.mx.Lock() |
|
defer st.mx.Unlock() |
|
ch := st.channels[chanID] |
|
if ch == nil { |
|
return nil |
|
} |
|
|
|
result := make([]Message, len(ch.Messages)) |
|
for i, msg := range ch.Messages { |
|
result[i] = *msg |
|
} |
|
|
|
return result |
|
} |
|
|
|
|
|
func (st *state) DeleteMessage(chanID, ts string) bool { |
|
st.mx.Lock() |
|
defer st.mx.Unlock() |
|
ch := st.channels[chanID] |
|
if ch == nil { |
|
return false |
|
} |
|
|
|
var deleted bool |
|
msgs := ch.Messages[:0] |
|
for _, m := range ch.Messages { |
|
if m.TS == ts { |
|
deleted = true |
|
continue |
|
} |
|
msgs = append(msgs, m) |
|
} |
|
ch.Messages = msgs |
|
|
|
return deleted |
|
} |
|
|
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
|
log.Printf("%s %s", req.Method, req.URL.Path) |
|
s.handler.ServeHTTP(w, req) |
|
} |
|
|