casdoor-ruby-sdk 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +134 -55
  3. data/lib/casdoor/adapter.rb +48 -0
  4. data/lib/casdoor/application.rb +48 -0
  5. data/lib/casdoor/auth.rb +143 -0
  6. data/lib/casdoor/cert.rb +47 -0
  7. data/lib/casdoor/email.rb +33 -0
  8. data/lib/casdoor/enforce.rb +47 -0
  9. data/lib/casdoor/enforcer.rb +48 -0
  10. data/lib/casdoor/entity.rb +17 -44
  11. data/lib/casdoor/global.rb +34 -0
  12. data/lib/casdoor/group.rb +48 -0
  13. data/lib/casdoor/invitation.rb +57 -0
  14. data/lib/casdoor/jwt.rb +75 -0
  15. data/lib/casdoor/ldap.rb +67 -0
  16. data/lib/casdoor/logout.rb +41 -0
  17. data/lib/casdoor/mfa.rb +53 -0
  18. data/lib/casdoor/model.rb +48 -0
  19. data/lib/casdoor/notification.rb +29 -0
  20. data/lib/casdoor/order.rb +59 -0
  21. data/lib/casdoor/order_pay.rb +40 -0
  22. data/lib/casdoor/organization.rb +47 -0
  23. data/lib/casdoor/payment.rb +61 -0
  24. data/lib/casdoor/permission.rb +57 -0
  25. data/lib/casdoor/plan.rb +48 -0
  26. data/lib/casdoor/policy.rb +67 -0
  27. data/lib/casdoor/pricing.rb +48 -0
  28. data/lib/casdoor/product.rb +48 -0
  29. data/lib/casdoor/provider.rb +48 -0
  30. data/lib/casdoor/record.rb +43 -0
  31. data/lib/casdoor/resource.rb +75 -0
  32. data/lib/casdoor/role.rb +52 -0
  33. data/lib/casdoor/session.rb +53 -0
  34. data/lib/casdoor/sms.rb +31 -0
  35. data/lib/casdoor/subscription.rb +48 -0
  36. data/lib/casdoor/syncer.rb +48 -0
  37. data/lib/casdoor/token.rb +60 -0
  38. data/lib/casdoor/transaction.rb +62 -0
  39. data/lib/casdoor/url.rb +47 -0
  40. data/lib/casdoor/user.rb +124 -0
  41. data/lib/casdoor/util.rb +147 -0
  42. data/lib/casdoor/util_modify.rb +65 -0
  43. data/lib/casdoor/version.rb +1 -1
  44. data/lib/casdoor/webhook.rb +48 -0
  45. data/lib/casdoor.rb +13 -5
  46. metadata +41 -6
  47. data/lib/casdoor/api/auth.rb +0 -153
  48. data/lib/casdoor/api/crud.rb +0 -122
  49. data/lib/casdoor/api/enforce.rb +0 -44
  50. data/lib/casdoor/api/users.rb +0 -83
  51. data/lib/casdoor/client.rb +0 -143
@@ -1,143 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright 2026 The Casdoor Authors. All Rights Reserved.
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- require 'json'
18
- require 'net/http'
19
- require 'uri'
20
-
21
- module Casdoor
22
- # The client of the Casdoor API. By default it calls the API as the application, authenticated by the client ID
23
- # and the client secret. Use with_access_token to call the API as a user instead.
24
- #
25
- # client = Casdoor::Client.new(
26
- # endpoint: "https://door.casdoor.com",
27
- # client_id: "...",
28
- # client_secret: "...",
29
- # certificate: File.read("token_jwt_key.pem"),
30
- # organization_name: "casbin",
31
- # application_name: "app-example"
32
- # )
33
- class Client
34
- include Api::Auth
35
- include Api::Crud
36
- include Api::Users
37
- include Api::Enforce
38
-
39
- attr_reader :endpoint, :client_id, :client_secret, :certificate, :organization_name, :application_name,
40
- :access_token, :headers, :open_timeout, :read_timeout
41
-
42
- # certificate is the public certificate (or public key) of the application's cert in Casdoor, in PEM format.
43
- # It's only needed by parse_jwt_token.
44
- # headers are added to all the requests, e.g. to pass a gateway.
45
- def initialize(endpoint:, client_id:, client_secret:, organization_name:, application_name:, certificate: nil,
46
- headers: {}, access_token: nil, open_timeout: 10, read_timeout: 30)
47
- @endpoint = endpoint.to_s.chomp('/')
48
- @client_id = client_id
49
- @client_secret = client_secret
50
- @certificate = certificate
51
- @organization_name = organization_name
52
- @application_name = application_name
53
- @headers = headers.dup.freeze
54
- @access_token = access_token
55
- @open_timeout = open_timeout
56
- @read_timeout = read_timeout
57
- end
58
-
59
- # Returns a copy of the client that calls the API as the user who owns the access token, instead of as the
60
- # application. The API is still subject to the permission check of Casdoor, so a normal user can only access
61
- # their own data:
62
- #
63
- # token = client.get_oauth_token(code)
64
- # user = client.with_access_token(token["access_token"]).get_account
65
- def with_access_token(access_token)
66
- Client.new(endpoint: endpoint, client_id: client_id, client_secret: client_secret,
67
- organization_name: organization_name, application_name: application_name, certificate: certificate,
68
- headers: headers, access_token: access_token, open_timeout: open_timeout, read_timeout: read_timeout)
69
- end
70
-
71
- def get_url(action, query = {})
72
- query = query.compact
73
- url = "#{endpoint}/api/#{action}"
74
- url += "?#{URI.encode_www_form(query)}" unless query.empty?
75
- URI(url)
76
- end
77
-
78
- # Calls a GET API, and returns the whole response like {"status" => "ok", "data" => ..., "data2" => ...}
79
- def get_response(action, query = {})
80
- request = Net::HTTP::Get.new(get_url(action, query))
81
- check_response(send_request(request))
82
- end
83
-
84
- # Calls a GET API, and returns the "data" of the response
85
- def get_data(action, query = {})
86
- get_response(action, query)['data']
87
- end
88
-
89
- # Calls a POST API with a JSON body, and returns the whole response
90
- def post_json(action, query, body)
91
- request = Net::HTTP::Post.new(get_url(action, query), 'Content-Type' => 'application/json')
92
- request.body = body.to_json
93
- check_response(send_request(request))
94
- end
95
-
96
- # Calls a POST API with a form body, and returns the whole response
97
- def post_form(action, query, form)
98
- request = Net::HTTP::Post.new(get_url(action, query))
99
- request.set_form_data(form)
100
- check_response(send_request(request))
101
- end
102
-
103
- private
104
-
105
- # Sends the request and returns the parsed JSON body. Errors of the HTTP layer are raised, while the
106
- # {"status": "error"} responses are left to the caller.
107
- def send_request(request, auth: true)
108
- if auth
109
- if access_token
110
- request['Authorization'] = "Bearer #{access_token}"
111
- else
112
- request.basic_auth(client_id.to_s, client_secret.to_s)
113
- end
114
- end
115
- request['Accept'] = 'application/json'
116
- headers.each { |key, value| request[key] = value }
117
-
118
- uri = request.uri
119
- response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https',
120
- open_timeout: open_timeout, read_timeout: read_timeout) do |http|
121
- http.request(request)
122
- end
123
-
124
- begin
125
- body = JSON.parse(response.body.to_s)
126
- rescue JSON::ParserError
127
- raise HttpError.new(response.code.to_i, response.body)
128
- end
129
-
130
- return body if response.is_a?(Net::HTTPSuccess)
131
- # Casdoor answers some errors with a non-2xx status and a JSON body that explains the error
132
- return body if body.is_a?(Hash) && (body['status'] == 'error' || body['error'])
133
-
134
- raise HttpError.new(response.code.to_i, response.body)
135
- end
136
-
137
- def check_response(body)
138
- raise ApiError, body['msg'].to_s if body.is_a?(Hash) && body['status'] == 'error'
139
-
140
- body
141
- end
142
- end
143
- end