didkit 0.0.2 → 0.0.3

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: d974c618ebe4702ec35b8f4e1465b3cc0664d1cf932fd8739ac4371f7c20caa2
4
+ data.tar.gz: 66479727000f96ef26d09074bd97cd59d43f14388ea213f25deb846c6e75cb3d
5
5
  SHA512:
6
- metadata.gz: 636c2e274e596338aa463971ab1dadac22fb655c67622cce15b65059a99e0bd91ef3ed5a78984c4c48f5ff1731287f8d3efe2d05bc0445871778b9911e3c2977
7
- data.tar.gz: 62c9f5ef2238320d598050ff00d696adf572e69c663d85b04424251f4664376193ce40d0dc7bf92daf8d9e315b6d245510ad560759aacb0452955452d3027d2f
6
+ metadata.gz: b3b84e04e7be85c0af2a544173a14338870fc3163727dd9341a1c76d83ce6397c03f4f28284549c5c07c2c3625e018b81c7a4149c80dac8e817f01f7b724c042
7
+ data.tar.gz: e476000c6bbc05a3e5664e2fb44a8bb19e302c2b13d880bfc9d98a2144916b7982d914d41beea55e5775f55a1a3d01a9bd0854ff4cfa9274d86aba4a406441e1
data/CHANGELOG.md CHANGED
@@ -1,2 +1,21 @@
1
- ## [Unreleased]
1
+ ## [0.0.3] - 2024-03-06
2
2
 
3
+ - added `Document#handles` with handle info extracted from `alsoKnownAs` field
4
+ - added validation of various fields of the DID document
5
+ - added `DID#resolved_by` (`:dns` or `:http`)
6
+ - added `DID#did` which returns the DID in a string form like `to_s`
7
+ - added `DID#web_domain` which returns the domain part of a `did:web`
8
+ - changed `DID#type` to be stored as a symbol
9
+
10
+ ## [0.0.2] - 2023-11-14
11
+
12
+ - fixed missing require
13
+ - fixed some connection error handling
14
+
15
+ ## [0.0.1] - 2023-11-14
16
+
17
+ Initial release:
18
+
19
+ - resolving handle to DID via DNS or HTTP well-known
20
+ - loading DID document via PLC or did:web well-known
21
+ - 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
@@ -12,9 +12,9 @@ module DIDKit
12
12
  domain = handle.gsub(/^@/, '')
13
13
 
14
14
  if dns_did = resolve_handle_by_dns(domain)
15
- DID.new(dns_did)
15
+ DID.new(dns_did, :dns)
16
16
  elsif http_did = resolve_handle_by_well_known(domain)
17
- DID.new(http_did)
17
+ DID.new(http_did, :http)
18
18
  else
19
19
  nil
20
20
  end
@@ -51,44 +51,43 @@ module DIDKit
51
51
  nil
52
52
  end
53
53
 
54
- attr_reader :type
54
+ attr_reader :type, :did, :resolved_by
55
55
 
56
- def initialize(did)
56
+ def initialize(did, resolved_by = nil)
57
57
  if did =~ /^did\:(\w+)\:/
58
58
  @did = did
59
- @type = $1
59
+ @type = $1.to_sym
60
60
  else
61
61
  raise DIDError.new("Invalid DID format")
62
62
  end
63
63
 
64
- if @type != 'plc' && @type != 'web'
64
+ if @type != :plc && @type != :web
65
65
  raise DIDError.new("Unrecognized DID type: #{@type}")
66
66
  end
67
- end
68
67
 
69
- def to_s
70
- @did
68
+ @resolved_by = resolved_by
71
69
  end
72
70
 
71
+ alias to_s did
72
+
73
73
  def get_document
74
- if @type == 'plc'
75
- resolve_did_plc(@did)
76
- elsif @type == 'web'
77
- resolve_did_web(@did)
78
- end
74
+ type == :plc ? resolve_did_plc : resolve_did_web
75
+ end
76
+
77
+ def web_domain
78
+ did.gsub(/^did\:web\:/, '') if type == :web
79
79
  end
80
80
 
81
- def resolve_did_plc(did)
81
+ def resolve_did_plc
82
82
  url = "https://plc.directory/#{did}"
83
83
  json = JSON.parse(URI.open(url).read)
84
- Document.new(json)
84
+ Document.new(self, json)
85
85
  end
86
86
 
87
- def resolve_did_web(did)
88
- host = did.gsub(/^did\:web\:/, '')
89
- url = "https://#{host}/.well-known/did.json"
87
+ def resolve_did_web
88
+ url = "https://#{web_domain}/.well-known/did.json"
90
89
  json = JSON.parse(URI.open(url).read)
91
- Document.new(json)
90
+ Document.new(self, json)
92
91
  end
93
92
  end
94
93
  end
@@ -1,14 +1,41 @@
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
7
- end
8
15
 
9
- def pds_endpoint
10
- service = (@json['service'] || []).detect { |s| s['id'] == '#atproto_pds' }
11
- service && service['serviceEndpoint']
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
12
39
  end
13
40
  end
14
41
  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.3"
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.3
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-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: