Skip to content

Instantly share code, notes, and snippets.

@mackuba
Created November 11, 2023 23:45
Show Gist options
  • Save mackuba/a6c2a922be8895c2b82c93e54272b86d to your computer and use it in GitHub Desktop.
Save mackuba/a6c2a922be8895c2b82c93e54272b86d to your computer and use it in GitHub Desktop.
Bluesky login with federation
# "proper" way, more future- and federation-proof
handle = form.get('handle')
password = form.get('password')
if dns_record = lookup_dns("_atproto.#{handle}")
did = did_from_dns(dns_record)
elsif res = open_url("https://#{handle}/.well-known/atproto-did")
did = did_from_well_known(res)
else
raise "handle could not be resolved"
end
if did.type == 'plc'
did_doc = open_url("https://plc.directory/#{did}")
elsif did.type == 'web'
did_doc = open_url("https://#{host}/.well-known/did.json")
else
raise "unknown did type"
end
pds_host = did_doc['service'][0]['serviceEndpoint']
client = Client.new(pds_host)
client.log_in(handle, password)
client.post("hello world")
# shortcut via appview
handle = form.get('handle')
password = form.get('password')
appview = Client.new('api.bsky.app')
did = appview.resolveHandle(handle)
if did.type == 'plc'
did_doc = open_url("https://plc.directory/#{did}")
elsif did.type == 'web'
did_doc = open_url("https://#{host}/.well-known/did.json")
else
raise "unknown did type"
end
pds_host = did_doc['service'][0]['serviceEndpoint']
client = Client.new(pds_host)
client.log_in(handle, password)
client.post("hello world")
# transitional way using bsky.social login, until full federation
handle = form.get('handle')
password = form.get('password')
social = Client.new('bsky.social')
res = social.log_in(handle, password)
pds_host = res['didDoc']['service'][0]['serviceEndpoint']
client = Client.new(pds_host, access: res['accessJwt'])
client.post("hello world")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment