Skip to content

Instantly share code, notes, and snippets.

@mackuba
Created November 11, 2023 23:45
Show Gist options
  • Select an option

  • Save mackuba/a6c2a922be8895c2b82c93e54272b86d to your computer and use it in GitHub Desktop.

Select an option

Save mackuba/a6c2a922be8895c2b82c93e54272b86d to your computer and use it in GitHub Desktop.

Revisions

  1. mackuba created this gist Nov 11, 2023.
    28 changes: 28 additions & 0 deletions proper_way.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    # "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")

    23 changes: 23 additions & 0 deletions using_appview.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    # 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")

    12 changes: 12 additions & 0 deletions using_bsky_social.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    # 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")