What's the official way of logging in with a p

2019-08-20 05:12发布

We're running two mattermost servers.

One we have a python process logging in with https://github.com/Vaelor/python-mattermost-driver using a personal access token with the community Python driver. This process has a session that doesn't time out which is one of the benefits of using a personal access token to login. https://docs.mattermost.com/developer/personal-access-tokens.html .

We log in using a username and password with the client4 go driver and this works however it times out after a while. It appears there is no way of using a personal access token to log in with the official client 4 driver.

The documentation for the Mattermost Client4 code is at https://godoc.org/github.com/mattermost/platform/model#Client

The source for client4 is at https://github.com/mattermost/mattermost-server/blob/master/model/client4.go

The closest thing that looks like it would work is logging in under with a username and password and then setting the authentication token via client.MockSession which failed on testing.

What's the official way of logging in with a personal access token using the client4 go driver for mattermost?

标签: go mattermost
1条回答
三岁会撩人
2楼-- · 2019-08-20 05:32

The key is not logging in, setting the personal access token and calling client.GetMe. This became clear after looking through the python driver source code. The relevant fragment is.

    fmt.Println("LoginAsTheBotUser", "Using personal access token")
    client.AuthToken = mattermost_personal_access_token
    client.AuthType = model.HEADER_TOKEN
    if user, resp := client.GetMe(""); resp.Error != nil {
        println("There was a problem logging into the Mattermost server.  Are you sure the access token is valid?")
        PrintError(resp.Error)
    } else {
        fmt.Println(client)
        botUser = user
        result = true
    }

Context.

/* 
    mattermost_user_email, mattermost_user_password and mattermost_personal_access_token
    are globals set from reading a .env file on startup
    */
func LoginAsTheBotUser() bool {
    fmt.Println("LoginAsTheBotUser")
    fmt.Println("LoginAsTheBotUser", time.Now())
    var result bool = false
    if mattermost_personal_access_token == "" {
        fmt.Println("LoginAsTheBotUser", "Getting access token from login with username and password")
        if user, resp := client.Login(mattermost_user_email, mattermost_user_password); resp.Error != nil {
            println("There was a problem logging into the Mattermost server.  Are you sure ran the setup steps from the README.md?")
            PrintError(resp.Error)
        } else {
            fmt.Println(client)
            botUser = user
            result = true
        }
    } else {
        fmt.Println("LoginAsTheBotUser", "Using personal access token")
        client.AuthToken = mattermost_personal_access_token
        client.AuthType = model.HEADER_TOKEN
        if user, resp := client.GetMe(""); resp.Error != nil {
            //if user, resp := client.Login(mattermost_personal_access_token); resp.Error != nil {
            println("There was a problem logging into the Mattermost server.  Are you sure the access token is valid?")
            PrintError(resp.Error)
        } else {
            fmt.Println(client)
            botUser = user
            result = true
        }
    }
    fmt.Println("LoginAsTheBotUser", time.Now())
    fmt.Println("LoginAsTheBotUser")
    return result
}
查看更多
登录 后发表回答