Module: OpenID

Included in:
Consumer, DiffieHelmanAssociator
Defined in:
lib/openid/util.rb,
lib/openid/errors.rb,
lib/openid/consumer.rb,
lib/openid/constants.rb,
lib/openid/interface.rb,
lib/openid/association.rb

Defined Under Namespace

Classes: Association, AssociationManager, AuthenticationError, BaseAssociationManager, Consumer, ConsumerAssociation, DiffieHelmanAssociator, DumbAssociationManager, NoArgumentsError, NoOpenIDArgs, ProtocolError, Request, Response, ServerAssociation, SimpleHTTPClient, UserCancelled, UserSetupNeeded, ValueMismatchError

Constant Summary collapse

SECRET_SIZES =
{'HMAC-SHA1' => 20 }
DEFAULT_DH_MODULUS =
155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443
DEFAULT_DH_GEN =
2

Instance Method Summary collapse

Instance Method Details

#append_args(url, args) ⇒ Object

Appends arguments in hash args to the existing url string



90
91
92
93
# File 'lib/openid/util.rb', line 90

def append_args(url, args)
	return url if args.empty?
	return url + (url.include? '?' and '&' or '?') + url_encode(args)
end

#error_page(body) ⇒ Object

Creates a response object signaling a plaintext error.



20
21
22
# File 'lib/openid/interface.rb', line 20

def error_page(body)
	return Response.new('code'=>400, 'content_type'=>'text/plain', 'body'=>body)
end

#from_btwoc(str) ⇒ Object

Converts a raw string containing a big endian two’s complement number into a Fixnum or Bignum



95
96
97
98
99
100
101
102
103
104
# File 'lib/openid/util.rb', line 95

def from_btwoc(str)
	# Maybe this should be part of a btwoc class or something
	str = "\000" * (4 - (str.length % 4)) + str
	num = 0
	str.unpack('N*').each { |x|
		num <<= 32
		num |= x
	}
	return num
end

#normalize_url(url) ⇒ Object

present.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/openid/consumer.rb', line 17

def normalize_url(url)
	url.strip!
	if (!url.index(/^http:\/\/|^https:\/\//))
		url = 'http://' + url
	end
	
	# TODO: Some unicode handling
	# (Keeping in mind that ruby's unicode/string distinction is kinda nil so far)
	
	return url
end

#parse_kv(d) ⇒ Object

Returns a hash.



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/openid/util.rb', line 107

def parse_kv(d)
	d.strip!
	args = {}
	d.split("\n").each { |line|
		pair = line.split(':',2)
		if pair.length == 2
			k, v = pair
			args[k.strip] = v.strip
		end
	}
	return args
end

Takes a string containing html, and yields the attributes of each link tags until a body tag is found.



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/openid/util.rb', line 120

def parse_link_attrs(data)
	parser = HTMLTokenizer.new(data)
	while el = parser.getTag('link', 'body')
		if el.tag_name == 'link'
			yield el.attr_hash
		elsif el.tag_name == 'body'
			return
		end
	end
	
end

#quote_minimal(s) ⇒ Object



11
12
13
14
# File 'lib/openid/consumer.rb', line 11

def quote_minimal(s)
	# TODO: implement? (Used by normalize_url's unicode handling in the python modules)
	return s
end

#redirect(url) ⇒ Object

Creates a Response object signaling a redirect to url.



12
13
14
# File 'lib/openid/interface.rb', line 12

def redirect(url)
	return Response.new('code'=>302, 'redirect_url'=>url.to_s)
end

#response_page(body) ⇒ Object

Creates a response object signaling a plaintext response.



16
17
18
# File 'lib/openid/interface.rb', line 16

def response_page(body)
	return Response.new('code'=>200, 'content_type'=>'text/plain', 'body'=>body)
end

#sign_reply(reply, key, signed_fields) ⇒ Object

Generates a signature for a set of fields. reply is a hash containing the data that was signed, key is the secret key, and signed_fields is an array containing the names (in order) of the fields to be signed. Returns the list of signed fields (comma separated) and the base64 encoded signature



136
137
138
139
140
141
142
143
# File 'lib/openid/util.rb', line 136

def sign_reply(reply, key, signed_fields)
	token = ''
	signed_fields.each { |x|
		token += x + ':' + reply['openid.' + x] + "\n"
	}
	d = Base64.encode64(HMAC::SHA1.digest(key,token)).delete("\n")
	return signed_fields.join(','), d
end

#url_encode(query) ⇒ Object

Takes all entries in a hash and combines and escapes them (using CGI::escape) into a single string.



81
82
83
84
85
86
87
88
# File 'lib/openid/util.rb', line 81

def url_encode(query)
	output = ''
	query.each { |key, val|
		output += CGI::escape(key.to_s) + '=' + CGI::escape(val.to_s) + '&'
	}
	output.chop!
	return output
end