Class: Nelumba::Identity

Inherits:
Object
  • Object
show all
Includes:
MongoMapper::Document
Defined in:
lib/nelumba-mongodb/identity.rb

Overview

This represents the information necessary to talk to an Person that is external to our node, or it represents how to talk to us. An Identity stores endpoints that are used to push or pull Activities from.

Constant Summary collapse

PUBLIC_KEY_LEASE_DAYS =

public keys are good for 4 weeks

28

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Identity

Returns a new instance of Identity.



6
# File 'lib/nelumba-mongodb/identity.rb', line 6

def initialize(*args); super(*args); end

Class Method Details

.create(*args) ⇒ Object

Create a new Identity from a Hash of values or a Nelumba::Identity.



134
135
136
# File 'lib/nelumba-mongodb/identity.rb', line 134

def self.create(*args)
  self.create! *args
end

.create!(*args) ⇒ Object

Create a new Identity from a Hash of values or a Nelumba::Identity. TODO: Create outbox and inbox aggregates to hold feed and sent activities



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/nelumba-mongodb/identity.rb', line 110

def self.create!(*args)
  hash = {}
  if args.length > 0
    hash = args.shift
  end

  if hash.is_a? Nelumba::Identity
    hash = hash.to_hash
  end

  hash["username"] = hash["username"].downcase if hash["username"]
  hash["username"] = hash[:username].downcase if hash[:username]
  hash.delete :username

  hash["domain"] = hash["domain"].downcase if hash["domain"]
  hash["domain"] = hash[:domain].downcase if hash[:domain]
  hash.delete :domain

  hash = self.sanitize_params(hash)

  super hash, *args
end

.discover!(account) ⇒ Object

Discover an identity from the given user identifier.



167
168
169
170
171
172
173
174
175
# File 'lib/nelumba-mongodb/identity.rb', line 167

def self.discover!()
  identity = Nelumba::Identity.find_by_identifier()
  return identity if identity

  identity = Nelumba.discover_identity()
  return false unless identity

  self.create!(identity)
end

.find_by_identifier(identifier) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/nelumba-mongodb/identity.rb', line 98

def self.find_by_identifier(identifier)
  matches  = identifier.match /^(?:.+\:)?([^@]+)@(.+)$/

  username = matches[1].downcase
  domain   = matches[2].downcase

  Nelumba::Identity.first(:username => username,
                        :domain => domain)
end

.new_local(person, username, domain, port, ssl, public_key) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/nelumba-mongodb/identity.rb', line 80

def self.new_local(person, username, domain, port, ssl, public_key)
  Nelumba::Identity.new(
    :username => username,
    :domain => domain,
    :ssl => ssl,
    :port => port,
    :person_id => person.id,
    :public_key => public_key,
    :salmon_endpoint => "/people/#{person.id}/salmon",
    :dialback_endpoint => "/people/#{person.id}/dialback",
    :activity_inbox_endpoint => "/people/#{person.id}/inbox",
    :activity_outbox_endpoint => "/people/#{person.id}/outbox",
    :profile_page => "/people/#{person.id}",
    :outbox_id => person.activities.id,
    :inbox_id => person.timeline.id
  )
end

.sanitize_params(params) ⇒ Object

Ensure params has only valid keys



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/nelumba-mongodb/identity.rb', line 139

def self.sanitize_params(params)
  params.keys.each do |k|
    if k.is_a? Symbol
      params[k.to_s] = params[k]
      params.delete k
    end
  end

  # Delete unknown keys
  params.keys.each do |k|
    unless self.keys.keys.include? k
      params.delete(k)
    end
  end

  # Delete immutable fields
  params.delete("_id")

  # Convert back to symbols
  params.keys.each do |k|
    params[k.intern] = params[k]
    params.delete k
  end

  params
end

Instance Method Details

#discover_person!Object

Discover the associated author for this identity.



178
179
180
# File 'lib/nelumba-mongodb/identity.rb', line 178

def discover_person!
  Person.discover!("acct:#{self.username}@#{self.domain}")
end

#invalidate_public_key!Object

Invalidates the public key



59
60
61
62
# File 'lib/nelumba-mongodb/identity.rb', line 59

def invalidate_public_key!
  self.public_key_lease = nil
  self.save
end

#post!(activity) ⇒ Object

Post an existing activity to the inbox of the person that owns this Identity



183
184
185
186
187
188
189
# File 'lib/nelumba-mongodb/identity.rb', line 183

def post!(activity)
  if self.person.local?
    self.person.local_deliver! activity
  else
    self.inbox.repost! activity
  end
end

#reset_key_leaseObject

Extends the lease for the public key so it remains valid through the given expiry period.



47
48
49
# File 'lib/nelumba-mongodb/identity.rb', line 47

def reset_key_lease
  self.public_key_lease = (DateTime.now + PUBLIC_KEY_LEASE_DAYS).to_date
end

#reset_key_lease!Object

Extends the lease for the public key so it remains valid through the given expiry period and saves.



53
54
55
56
# File 'lib/nelumba-mongodb/identity.rb', line 53

def reset_key_lease!
  reset_key_lease
  self.save
end

#return_or_discover_public_keyObject

Returns the valid public key



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/nelumba-mongodb/identity.rb', line 65

def return_or_discover_public_key
  if self.public_key_lease.nil? or
     self.public_key_lease < DateTime.now.to_date
    # Lease has expired, get the public key again
    identity = Nelumba.discover_identity("acct:#{self.username}@#{self.domain}")

    self.public_key = identity.public_key
    reset_key_lease

    self.save
  end

  self.public_key
end