8784: Fix test for latest firefox.
[arvados.git] / services / api / script / get_anonymous_user_token.rb
1 #!/usr/bin/env ruby
2
3 # Get or Create an anonymous user token.
4 # If get option is used, an existing anonymous user token is returned. If none exist, one is created.
5 # If the get option is omitted, a new token is created and returned.
6
7 require 'trollop'
8
9 opts = Trollop::options do
10   banner ''
11   banner "Usage: get_anonymous_user_token "
12   banner ''
13   opt :get, <<-eos
14 Get an existing anonymous user token. If no such token exists \
15 or if this option is omitted, a new token is created and returned.
16   eos
17 end
18
19 get_existing = opts[:get]
20
21 require File.dirname(__FILE__) + '/../config/environment'
22
23 include ApplicationHelper
24 act_as_system_user
25
26 def create_api_client_auth
27   api_client_auth = ApiClientAuthorization.
28     new(user: anonymous_user,
29         api_client_id: 0,
30         expires_at: Time.now + 100.years,
31         scopes: ['GET /'])
32   api_client_auth.save!
33   api_client_auth.reload
34 end
35
36 if get_existing
37   api_client_auth = ApiClientAuthorization.
38     where('user_id=?', anonymous_user.id.to_i).
39     where('expires_at>?', Time.now).
40     select { |auth| auth.scopes == ['GET /'] }.
41     first
42 end
43
44 # either not a get or no api_client_auth was found
45 if !api_client_auth
46   api_client_auth = create_api_client_auth
47 end
48
49 # print it to the console
50 puts api_client_auth.api_token