|
| 1 | +// Copyright 2026 The casbin Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package controllers |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + |
| 20 | + "github.com/apache/casbin-gateway/imbridge" |
| 21 | + "github.com/apache/casbin-gateway/object" |
| 22 | +) |
| 23 | + |
| 24 | +// imChannelView is one stored channel and what its listener is doing. |
| 25 | +type imChannelView struct { |
| 26 | + *object.ImChannel |
| 27 | + Status imbridge.Status `json:"status"` |
| 28 | +} |
| 29 | + |
| 30 | +// GetImChannels lists the chat platforms Gateway listens on. Every credential is |
| 31 | +// masked: a token stored here never goes back to the browser. |
| 32 | +func (c *ApiController) GetImChannels() { |
| 33 | + if c.RequireAdmin() { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + channels, err := object.GetImChannels() |
| 38 | + if err != nil { |
| 39 | + c.ResponseError(err.Error()) |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + statuses := map[string]imbridge.Status{} |
| 44 | + for _, status := range imbridge.Statuses() { |
| 45 | + statuses[status.Name] = status |
| 46 | + } |
| 47 | + |
| 48 | + result := []*imChannelView{} |
| 49 | + for _, channel := range channels { |
| 50 | + result = append(result, &imChannelView{ImChannel: channel.Masked(), Status: statuses[channel.Name]}) |
| 51 | + } |
| 52 | + c.ResponseOk(result) |
| 53 | +} |
| 54 | + |
| 55 | +// UpdateImChannel writes one channel and starts or stops its listener to match. |
| 56 | +func (c *ApiController) UpdateImChannel() { |
| 57 | + if c.RequireAdmin() { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + channel := object.ImChannel{} |
| 62 | + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &channel); err != nil { |
| 63 | + c.ResponseError(err.Error()) |
| 64 | + return |
| 65 | + } |
| 66 | + if err := object.SaveImChannel(&channel); err != nil { |
| 67 | + c.ResponseError(err.Error()) |
| 68 | + return |
| 69 | + } |
| 70 | + c.ResponseOk(channel.Masked()) |
| 71 | +} |
| 72 | + |
| 73 | +func (c *ApiController) DeleteImChannel() { |
| 74 | + if c.RequireAdmin() { |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + var request struct { |
| 79 | + Name string `json:"name"` |
| 80 | + } |
| 81 | + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &request); err != nil { |
| 82 | + c.ResponseError(err.Error()) |
| 83 | + return |
| 84 | + } |
| 85 | + if err := object.DeleteImChannel(request.Name); err != nil { |
| 86 | + c.ResponseError(err.Error()) |
| 87 | + return |
| 88 | + } |
| 89 | + c.ResponseOk() |
| 90 | +} |
| 91 | + |
| 92 | +// StartWeixinLogin asks WeChat for a code to be scanned by the account that will |
| 93 | +// carry the bot. |
| 94 | +func (c *ApiController) StartWeixinLogin() { |
| 95 | + if c.RequireAdmin() { |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + qrcode, err := imbridge.StartWeixinLogin(c.Ctx.Request.Context()) |
| 100 | + if err != nil { |
| 101 | + c.ResponseError(err.Error()) |
| 102 | + return |
| 103 | + } |
| 104 | + c.ResponseOk(qrcode) |
| 105 | +} |
| 106 | + |
| 107 | +// GetWeixinLoginStatus reports whether the code has been scanned. The token that |
| 108 | +// comes back is written straight into the named channel rather than handed to |
| 109 | +// the browser: it is the credential, and it has no business making that trip. |
| 110 | +func (c *ApiController) GetWeixinLoginStatus() { |
| 111 | + if c.RequireAdmin() { |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + status, err := imbridge.PollWeixinLogin(c.Ctx.Request.Context(), c.Input().Get("qrcode")) |
| 116 | + if err != nil { |
| 117 | + c.ResponseError(err.Error()) |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + if status.Token != "" { |
| 122 | + channel, err := object.GetImChannel(c.Input().Get("channel")) |
| 123 | + if err != nil { |
| 124 | + c.ResponseError(err.Error()) |
| 125 | + return |
| 126 | + } |
| 127 | + if channel == nil { |
| 128 | + c.ResponseError("no channel is waiting for this sign-in") |
| 129 | + return |
| 130 | + } |
| 131 | + channel.Token = status.Token |
| 132 | + if err := object.SaveImChannel(channel); err != nil { |
| 133 | + c.ResponseError(err.Error()) |
| 134 | + return |
| 135 | + } |
| 136 | + status.Token = "" |
| 137 | + } |
| 138 | + c.ResponseOk(status) |
| 139 | +} |
0 commit comments