didkit 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04d52ea6bded9fd573e1d4f5794b1b46691159982228b699e32e7b6d83c73573
4
- data.tar.gz: e866504833f5abd3cdc6d6225836eb5d6388415a0640439e57ec6a535226ade0
3
+ metadata.gz: 76dd4f3787cf1cc288015511eb35ea6d41ef4c390f85ca67854617fe7c46d710
4
+ data.tar.gz: 40ea426b53fa3176dccd4c538cbd40050a3883bb6632af2b45ca1a6f49ac84b7
5
5
  SHA512:
6
- metadata.gz: 636c2e274e596338aa463971ab1dadac22fb655c67622cce15b65059a99e0bd91ef3ed5a78984c4c48f5ff1731287f8d3efe2d05bc0445871778b9911e3c2977
7
- data.tar.gz: 62c9f5ef2238320d598050ff00d696adf572e69c663d85b04424251f4664376193ce40d0dc7bf92daf8d9e315b6d245510ad560759aacb0452955452d3027d2f
6
+ metadata.gz: a26c81e89e2397bb7690149e143a0de6c9e9635a993e8cd13d572d76b21b38ce37628eb3c3ebc4ea9ffc20b3807f619acb46612a76af9f816b336315a10f7aab
7
+ data.tar.gz: 89b4d9eb1ec2b7f5207587cde9b0a2b2db8a5912a92dc1ef996f0bac493996aae8b1340fc673838c404ac28d4ab191be6e7ce3a984f35a83402b9ef489feea8b
data/CHANGELOG.md CHANGED
@@ -1,2 +1,27 @@
1
- ## [Unreleased]
1
+ ## [0.0.4] - 2024-03-07
2
2
 
3
+ - extracted resolving code from `DID` to a new `Resolver` class (`DID` has helper methods to call the resolver)
4
+ - added `Resolver#get_validated_handle` method to validate handles from the `Document` (+ helpers in `DID` in `Document`)
5
+ - added timeout to `#resolve_handle_by_well_known`
6
+
7
+ ## [0.0.3] - 2024-03-06
8
+
9
+ - added `Document#handles` with handle info extracted from `alsoKnownAs` field
10
+ - added validation of various fields of the DID document
11
+ - added `DID#resolved_by` (`:dns` or `:http`)
12
+ - added `DID#did` which returns the DID in a string form like `to_s`
13
+ - added `DID#web_domain` which returns the domain part of a `did:web`
14
+ - changed `DID#type` to be stored as a symbol
15
+
16
+ ## [0.0.2] - 2023-11-14
17
+
18
+ - fixed missing require
19
+ - fixed some connection error handling
20
+
21
+ ## [0.0.1] - 2023-11-14
22
+
23
+ Initial release:
24
+
25
+ - resolving handle to DID via DNS or HTTP well-known
26
+ - loading DID document via PLC or did:web well-known
27
+ - extracting PDS endpoint field from the DID doc
data/README.md CHANGED
@@ -1,31 +1,22 @@
1
- # Didkit
1
+ # DidKit
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ A small Ruby gem for handling Distributed Identifiers (DIDs) in Bluesky / AT Protocol
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/didkit`. To experiment with that code, run `bin/console` for an interactive prompt.
6
5
 
7
- ## Installation
8
-
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
-
11
- Install the gem and add to the application's Gemfile by executing:
6
+ ## What does it do
12
7
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
8
+ **TODO** - not much yet :)
14
9
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
10
+ See the [did.rb](https://github.com/mackuba/didkit/blob/master/lib/didkit/did.rb) file for now.
16
11
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
12
 
19
- ## Usage
20
-
21
- TODO: Write usage instructions here
13
+ ## Installation
22
14
 
23
- ## Development
15
+ gem install didkit
24
16
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
17
 
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
18
+ ## Credits
28
19
 
29
- ## Contributing
20
+ Copyright © 2023 Kuba Suder ([@mackuba.eu](https://bsky.app/profile/mackuba.eu)).
30
21
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/didkit.
22
+ The code is available under the terms of the [zlib license](https://choosealicense.com/licenses/zlib/) (permissive, similar to MIT).
data/lib/didkit/did.rb CHANGED
@@ -1,94 +1,51 @@
1
- require 'json'
2
- require 'net/http'
3
- require 'open-uri'
4
- require 'resolv'
5
-
6
- require_relative 'document'
7
1
  require_relative 'errors'
2
+ require_relative 'resolver'
8
3
 
9
4
  module DIDKit
10
5
  class DID
11
6
  def self.resolve_handle(handle)
12
- domain = handle.gsub(/^@/, '')
13
-
14
- if dns_did = resolve_handle_by_dns(domain)
15
- DID.new(dns_did)
16
- elsif http_did = resolve_handle_by_well_known(domain)
17
- DID.new(http_did)
18
- else
19
- nil
20
- end
7
+ Resolver.new.resolve_handle(handle)
21
8
  end
22
9
 
23
- def self.resolve_handle_by_dns(domain)
24
- dns_records = Resolv::DNS.open { |d| d.getresources("_atproto.#{domain}", Resolv::DNS::Resource::IN::TXT) }
25
-
26
- if record = dns_records.first
27
- if string = record.strings.first
28
- if string =~ /^did\=(did\:\w+\:.*)$/
29
- return $1
30
- end
31
- end
32
- end
33
-
34
- nil
35
- end
36
-
37
- def self.resolve_handle_by_well_known(domain)
38
- url = URI("https://#{domain}/.well-known/atproto-did")
39
- response = Net::HTTP.get_response(url)
40
-
41
- if response.is_a?(Net::HTTPSuccess)
42
- if text = response.body
43
- if text.lines.length == 1 && text.start_with?('did:')
44
- return text
45
- end
46
- end
47
- end
10
+ attr_reader :type, :did, :resolved_by
48
11
 
49
- nil
50
- rescue StandardError => e
51
- nil
52
- end
53
-
54
- attr_reader :type
55
-
56
- def initialize(did)
12
+ def initialize(did, resolved_by = nil)
57
13
  if did =~ /^did\:(\w+)\:/
58
14
  @did = did
59
- @type = $1
15
+ @type = $1.to_sym
60
16
  else
61
17
  raise DIDError.new("Invalid DID format")
62
18
  end
63
19
 
64
- if @type != 'plc' && @type != 'web'
20
+ if @type != :plc && @type != :web
65
21
  raise DIDError.new("Unrecognized DID type: #{@type}")
66
22
  end
67
- end
68
23
 
69
- def to_s
70
- @did
24
+ @resolved_by = resolved_by
71
25
  end
72
26
 
27
+ alias to_s did
28
+
73
29
  def get_document
74
- if @type == 'plc'
75
- resolve_did_plc(@did)
76
- elsif @type == 'web'
77
- resolve_did_web(@did)
78
- end
30
+ Resolver.new.resolve_did(self)
31
+ end
32
+
33
+ def get_validated_handle
34
+ Resolver.new.get_validated_handle(self)
79
35
  end
80
36
 
81
- def resolve_did_plc(did)
82
- url = "https://plc.directory/#{did}"
83
- json = JSON.parse(URI.open(url).read)
84
- Document.new(json)
37
+ def web_domain
38
+ did.gsub(/^did\:web\:/, '') if type == :web
85
39
  end
86
40
 
87
- def resolve_did_web(did)
88
- host = did.gsub(/^did\:web\:/, '')
89
- url = "https://#{host}/.well-known/did.json"
90
- json = JSON.parse(URI.open(url).read)
91
- Document.new(json)
41
+ def ==(other)
42
+ if other.is_a?(String)
43
+ self.did == other
44
+ elsif other.is_a?(DID)
45
+ self.did == other.did
46
+ else
47
+ false
48
+ end
92
49
  end
93
50
  end
94
51
  end
@@ -1,14 +1,45 @@
1
1
  module DIDKit
2
2
  class Document
3
- attr_reader :json
3
+ class FormatError < StandardError
4
+ end
5
+
6
+ attr_reader :json, :did, :pds_endpoint, :handles
4
7
 
5
- def initialize(json)
8
+ def initialize(did, json)
9
+ raise FormatError, "Missing id field" if json['id'].nil?
10
+ raise FormatError, "Invalid id field" unless json['id'].is_a?(String)
11
+ raise FormatError, "id field doesn't match expected DID" unless json['id'] == did.to_s
12
+
13
+ @did = did
6
14
  @json = json
15
+
16
+ service = json['service']
17
+ raise FormatError, "Missing service key" if service.nil?
18
+ raise FormatError, "Invalid service data" unless service.is_a?(Array) && service.all? { |x| x.is_a?(Hash) }
19
+
20
+ if pds = service.detect { |x| x['id'] == '#atproto_pds' }
21
+ raise FormatError, "Missing PDS type" unless pds['type']
22
+ raise FormatError, "Invalid PDS type" unless pds['type'] == 'AtprotoPersonalDataServer'
23
+ raise FormatError, "Missing PDS endpoint" unless pds['serviceEndpoint']
24
+ raise FormatError, "Invalid PDS endpoint" unless pds['serviceEndpoint'].is_a?(String)
25
+ raise FormatError, "Invalid PDS endpoint" unless pds['serviceEndpoint'] =~ %r(://)
26
+
27
+ @pds_endpoint = pds['serviceEndpoint']
28
+ end
29
+
30
+ if aka = json['alsoKnownAs']
31
+ raise FormatError, "Invalid alsoKnownAs" unless aka.is_a?(Array)
32
+ raise FormatError, "Invalid alsoKnownAs" unless aka.all? { |x| x.is_a?(String) }
33
+ raise FormatError, "Invalid alsoKnownAs" unless aka.all? { |x| x =~ %r(\Aat://[^/]+\z) }
34
+
35
+ @handles = aka.map { |x| x.gsub('at://', '') }
36
+ else
37
+ @handles = []
38
+ end
7
39
  end
8
40
 
9
- def pds_endpoint
10
- service = (@json['service'] || []).detect { |s| s['id'] == '#atproto_pds' }
11
- service && service['serviceEndpoint']
41
+ def get_validated_handle
42
+ Resolver.new.get_validated_handle(self)
12
43
  end
13
44
  end
14
45
  end
@@ -0,0 +1,82 @@
1
+ require 'json'
2
+ require 'open-uri'
3
+ require 'net/http'
4
+ require 'resolv'
5
+
6
+ require_relative 'did'
7
+ require_relative 'document'
8
+
9
+ module DIDKit
10
+ class Resolver
11
+ def resolve_handle(handle)
12
+ domain = handle.gsub(/^@/, '')
13
+
14
+ if dns_did = resolve_handle_by_dns(domain)
15
+ DID.new(dns_did, :dns)
16
+ elsif http_did = resolve_handle_by_well_known(domain)
17
+ DID.new(http_did, :http)
18
+ else
19
+ nil
20
+ end
21
+ end
22
+
23
+ def resolve_handle_by_dns(domain)
24
+ dns_records = Resolv::DNS.open { |d| d.getresources("_atproto.#{domain}", Resolv::DNS::Resource::IN::TXT) }
25
+
26
+ if record = dns_records.first
27
+ if string = record.strings.first
28
+ if string =~ /^did\=(did\:\w+\:.*)$/
29
+ return $1
30
+ end
31
+ end
32
+ end
33
+
34
+ nil
35
+ end
36
+
37
+ def resolve_handle_by_well_known(domain)
38
+ url = URI("https://#{domain}/.well-known/atproto-did")
39
+
40
+ response = Net::HTTP.start(url.host, url.port, use_ssl: true, open_timeout: 10, read_timeout: 10) do |http|
41
+ request = Net::HTTP::Get.new(url)
42
+ http.request(request)
43
+ end
44
+
45
+ if response.is_a?(Net::HTTPSuccess)
46
+ if text = response.body
47
+ if text.lines.length == 1 && text.start_with?('did:')
48
+ return text
49
+ end
50
+ end
51
+ end
52
+
53
+ nil
54
+ rescue StandardError => e
55
+ nil
56
+ end
57
+
58
+ def resolve_did(did)
59
+ did = DID.new(did) if did.is_a?(String)
60
+
61
+ did.type == :plc ? resolve_did_plc(did) : resolve_did_web(did)
62
+ end
63
+
64
+ def resolve_did_plc(did)
65
+ url = "https://plc.directory/#{did}"
66
+ json = JSON.parse(URI.open(url).read)
67
+ Document.new(did, json)
68
+ end
69
+
70
+ def resolve_did_web(did)
71
+ url = "https://#{did.web_domain}/.well-known/did.json"
72
+ json = JSON.parse(URI.open(url).read)
73
+ Document.new(did, json)
74
+ end
75
+
76
+ def get_validated_handle(did_or_doc)
77
+ document = did_or_doc.is_a?(Document) ? did_or_doc : resolve_did(did_or_doc)
78
+
79
+ document.handles.detect { |h| resolve_handle(h) == document.did }
80
+ end
81
+ end
82
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DIDKit
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.4"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: didkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kuba Suder
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-14 00:00:00.000000000 Z
11
+ date: 2024-03-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -24,6 +24,7 @@ files:
24
24
  - lib/didkit/did.rb
25
25
  - lib/didkit/document.rb
26
26
  - lib/didkit/errors.rb
27
+ - lib/didkit/resolver.rb
27
28
  - lib/didkit/version.rb
28
29
  - sig/didkit.rbs
29
30
  homepage: https://github.com/mackuba/didkit