25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00
xmpp.chapril.org-ejabberd/test/ejabberd_oauth_mock.exs
2016-04-01 11:13:48 +02:00

48 lines
1.7 KiB
Elixir

# ----------------------------------------------------------------------
#
# ejabberd, Copyright (C) 2002-2016 ProcessOne
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# ----------------------------------------------------------------------
defmodule EjabberdOauthMock do
@author "jsautret@process-one.net"
def init() do
:mnesia.start
:mnesia.create_table(:oauth_token,
[ram_copies: [node],
attributes: [:oauth_token, :us, :scope, :expire]])
end
def get_token(user, domain, command, expiration \\ 3600) do
now = {megasecs, secs, _} = :os.timestamp
expire = 1000000 * megasecs + secs + expiration
:random.seed now
token = to_string :random.uniform(100000000)
{:ok, _} = :ejabberd_oauth.associate_access_token(token,
[{"resource_owner",
{:user, user, domain}},
{"scope", [to_string command]},
{"expiry_time", expire}],
:undefined)
token
end
end