Download this file
| 1 | package notion |
| 2 | |
| 3 | import "context" |
| 4 | |
| 5 | // Verify checks a token and returns an identifier for what it is bound |
| 6 | // to, so a connector row can be told apart from one pointing elsewhere. |
| 7 | // |
| 8 | // Called when a token is saved, so a bad one fails at the form rather |
| 9 | // than part-way through an import. |
| 10 | // |
| 11 | // The identifier is the BOT USER ID first, and the workspace name only |
| 12 | // as a fallback. Notion's /v1/users/me does not expose a workspace id; |
| 13 | // the name is what it gives, and a name collides between two workspaces |
| 14 | // called the same thing and changes when either is renamed. The bot id |
| 15 | // is unique to this integration and stable, which is the property |
| 16 | // actually wanted — the caller keys it anyway, so neither reaches disk |
| 17 | // in plaintext. |
| 18 | func Verify(ctx context.Context, token string) (string, error) { |
| 19 | c, err := New(token) |
| 20 | if err != nil { |
| 21 | return "", err |
| 22 | } |
| 23 | return c.verify(ctx) |
| 24 | } |
| 25 | |
| 26 | func (c *Client) verify(ctx context.Context) (string, error) { |
| 27 | var out struct { |
| 28 | ID string `json:"id"` |
| 29 | Name string `json:"name"` |
| 30 | Bot struct { |
| 31 | WorkspaceName string `json:"workspace_name"` |
| 32 | } `json:"bot"` |
| 33 | } |
| 34 | if err := c.get(ctx, "/v1/users/me", &out); err != nil { |
| 35 | return "", err |
| 36 | } |
| 37 | return firstNonEmpty(out.ID, out.Bot.WorkspaceName, out.Name), nil |
| 38 | } |
| 39 | |