Skip to content

Commit

Permalink
passwordauth
Browse files Browse the repository at this point in the history
  • Loading branch information
sdfsdhgjkbmnmxc committed Sep 20, 2020
1 parent 8e75f5c commit e20fa31
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 16 deletions.
40 changes: 28 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@

Simple client library.

## Examples
### First example: get server version

All examples have `-verbose` and `-server` options.
```
go run examples/serverinfo/main.go
go run examples/serverinfo/main.go -verbose
go run examples/serverinfo/main.go -verbose -server https://demo.tada.team
```

How to get server url, team uid and chat jid:
Any example have `-verbose` and `-server` options. Default server value is `https://web.tada.team`.

```https://demo.tada.team/dbd248d7-25c2-4e8f-a23a-99baf63223e9/chats/g-dce6f5fd-b741-40a6-aa9c-c0e928d9dac5```
### Authorization token

* https://demo.tada.team — server url
* dbd248d7-25c2-4e8f-a23a-99baf63223e9 – team uid
* g-dce6f5fd-b741-40a6-aa9c-c0e928d9dac5 – chat jid
Some examples need authorization token. You have two options:

#### Bot token

How to get bot token: type "/newbot <NAME>" command in @TadaBot direct chat.
Create bot by typing `/newbot <BOTNAME>` command in @TadaBot direct chat.
Allowed for team admin only.

### Server version
#### Token from your personal account

For server with sms authorization:
```
go run examples/serverinfo/main.go
go run examples/serverinfo/main.go -verbose
go run examples/serverinfo/main.go -verbose -server https://demo.tada.team
go run examples/smsauth/main.go
```

For server with Active Directory authorization:
```
go run examples/passwordauth/main.go
```

### Messaging
Expand All @@ -30,6 +39,13 @@ go run examples/serverinfo/main.go -verbose -server https://demo.tada.team
go run examples/message/main.go -team <team uid> -token <bot token> -chat <chat jid> -message <message text>
```

How to get team uid and chat jid:

```https://demo.tada.team/dbd248d7-25c2-4e8f-a23a-99baf63223e9/chats/g-dce6f5fd-b741-40a6-aa9c-c0e928d9dac5```

* dbd248d7-25c2-4e8f-a23a-99baf63223e9 – team uid
* g-dce6f5fd-b741-40a6-aa9c-c0e928d9dac5 – chat jid

### Contacts

```go run examples/contacts/main.go -team <team uid> -token <bot token>```
40 changes: 40 additions & 0 deletions examples/passwordauth/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"fmt"

"github.com/manifoldco/promptui"
"github.com/tada-team/tdclient"
"github.com/tada-team/tdclient/examples"
)

func main() {
settings := examples.NewSettings()
settings.Parse()

client, err := tdclient.NewSession(settings.Server)
if err != nil {
panic(err)
}

client.SetVerbose(settings.Verbose)

prompt := promptui.Prompt{Label: "Enter login"}
login, err := prompt.Run()
if err != nil {
panic(err)
}

prompt = promptui.Prompt{Label: "Enter password", Mask: '*'}
password, err := prompt.Run()
if err != nil {
panic(err)
}

tokenResp, err := client.AuthByPasswordGetToken(login, password)
if err != nil {
panic(err)
}

fmt.Println("Your token:", tokenResp.Token)
}
20 changes: 19 additions & 1 deletion json.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package tdclient

import jsoniter "github.com/json-iterator/go"
import (
"bytes"
"log"

jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)

var JSON = jsoniter.ConfigCompatibleWithStandardLibrary

func debugJSON(v interface{}) string {
b := new(bytes.Buffer)
debugEncoder := JSON.NewEncoder(b)
debugEncoder.SetIndent("", " ")
debugEncoder.SetEscapeHTML(false)
err := debugEncoder.Encode(v)
if err != nil {
log.Panicln(errors.Wrap(err, "json marshall fail"))
}
return b.String()
}
4 changes: 1 addition & 3 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ func (s Session) doRaw(method, path string, data, v interface{}) error {
return errors.Wrap(err, "read body fail")
}

if resp.StatusCode != 200 {
return errors.Wrapf(err, "status code: %d %s", resp.StatusCode, string(respData))
}
s.logger.Println("resp:", resp.StatusCode, string(respData))

if err := JSON.Unmarshal(respData, &v); err != nil {
return errors.Wrapf(err, "unmarshal fail on: %s", string(respData))
Expand Down
22 changes: 22 additions & 0 deletions v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,25 @@ func (s Session) AuthBySmsGetToken(phone, code string) (tdapi.Auth, error) {

return resp.Result, nil
}

func (s Session) AuthByPasswordGetToken(login, password string) (tdapi.Auth, error) {
req := map[string]interface{}{
"login": login,
"password": password,
}

resp := new(struct {
tdapi.Resp
Result tdapi.Auth `json:"result"`
})

if err := s.doPost("/api/v4/auth/password/get-token", req, resp); err != nil {
return resp.Result, err
}

if !resp.Ok {
return resp.Result, resp.Error
}

return resp.Result, nil
}

0 comments on commit e20fa31

Please sign in to comment.