2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: AGPL-3.0
6 # Get or Create an anonymous user token.
7 # If get option is used, an existing anonymous user token is returned. If none exist, one is created.
8 # If the get option is omitted, a new token is created and returned.
12 opts = Optimist::options do
14 banner "Usage: get_anonymous_user_token "
17 Get an existing anonymous user token. If no such token exists \
18 or if this option is omitted, a new token is created and returned.
20 opt :token, "token to create (optional)", :type => :string
23 get_existing = opts[:get]
24 supplied_token = opts[:token]
26 require File.dirname(__FILE__) + '/../config/environment'
28 include ApplicationHelper
31 def create_api_client_auth(supplied_token=nil)
33 # If token is supplied, see if it exists
35 api_client_auth = ApiClientAuthorization.
36 where(api_token: supplied_token).
39 # fall through to create a token
41 raise "Token exists, aborting!"
45 api_client_auth = ApiClientAuthorization.
46 new(user: anonymous_user,
48 expires_at: Time.now + 100.years,
50 api_token: supplied_token)
52 api_client_auth.reload
57 api_client_auth = ApiClientAuthorization.
58 where('user_id=?', anonymous_user.id.to_i).
59 where('expires_at>?', Time.now).
60 select { |auth| auth.scopes == ['GET /'] }.
64 # either not a get or no api_client_auth was found
66 api_client_auth = create_api_client_auth(supplied_token)
69 # print it to the console
70 puts api_client_auth.api_token