Is is possible to get an Id per user of my extension?

I am working on an extension that fetch some data from a remote server ( this data enriches the current scene ).
Each user has some limited access to the data, so she or he must be authenticated.

I have read the SU API but I see no means to get a persistent Id per user. There is some mention of extension accessing end user data in the developer terms of service, but I see it nowhere in the doc. Did I miss something ?

SketchUcation does it.
Its ExtensionStore³ dialog has an initial login/password to access the SCF server.
On its first run logging into the server, it receives a string from the server, and adds it as an encrypted string into a ‘cookie-like’ file stored in the user’s ‘Local’ folder structure.
When they next use the dialog that file is read and the encrypted string is sent to the server to authenticate the user without another login/password.
If for some reason that file is lost or the user changes, then they just need to repeat the login/password process…

thank you, I may have to use this kind of solution but having my own system is a it of a bother.

When I log in the extension warehouse, my profile displays all the extension I have, so there is clearly an auth system there. It’s a shame to duplicate it, instead of just of connecting to it.

Let see,… some things of interest to you:

(1) I think the Trimble sites use OAuth2 but do not expose what they are using to the World. (It’d be unsecure then.)


(2) You can access the platform’s environment variables via a hash-like object referenced as ENV
So for example:

user = ENV["USER"] || ENV["USERNAME"]

… which is short for:

if RUBY_PLATFORM !~ /(darwin)/
  user = ENV["USERNAME"] # MS Windows
else
  user = ENV["USER"] # it's OSX
end

… or written this way:

user =( RUBY_PLATFORM !~ /(darwin)/ ? ENV["USERNAME"] : ENV["USER"] )

or this way:

IS_WIN = Sketchup.platform == :platform_win
user =( IS_WIN ? ENV["USERNAME"] : ENV["USER"] )

* Use prettyprint to view the ENV hash in the console:

require "prettyprint"
pp ENV

>> a nicely formatted inspection of ENV is output


(3) The extension ID is (where ext_ref is a reference to your SketchupExtension instance object):
See: Homepage | SketchUp Developer

eid = ext_ref.id

(4) You can save extension specific data into the SketchUp User Hive of the registry (or plist on Mac) via the Sketchup::write_default method, and read them back via Sketchup::read_default :

@plug_key = "CompanyName_PluginName"
Sketchup::write_default( @plug_key, "user_id", "some_id_that_only_this_user_has" )

Here is another trick you can use to generate a guid to use as a user_id:

model = Sketchup::active_model
grp = model.entities.add_group
user_id = grp.guid
grp.erase! rescue nil

Or… use a standard Ruby library, like SecureRandom, which does not temporarily change the model:
http://ruby-doc.org/stdlib-2.0.0/libdoc/securerandom/rdoc/index.html
(This only works on SketchUp 2014 or higher, distributed with the Standard Ruby library.)

require "securerandom"

>> true

user_id = SecureRandom::uuid

>> 72a87198-e5a4-4816-a3d4-3101ef7f46e8

(The generated id should be different each time.)


… then save this generated user_id as shown above in (4).

Beware - this uses OpenSSL to generate random numbers and the version running on Ruby Windows uses some old Windows Debug API functions to seed this. These functions have become incredible slow over recent Windows versions and when you have a large model loaded it makes this lock up SketchUp for several minutes. There’s an open issue with the OpenSSL implementation that is still unresolved.

1 Like

Thanks, Thomas.

What about a similar trick is to use a new blank model’s guid, then open a new model ?

model = Sketchup::active_model
user_id = model.guid
Sketchup::file_new
if model.respond_to?(:close)
  model.close if RUBY_PLATFORM =~ /(darwin)/
end

My impression is that the OP is looking for a way to id a unique user. That would yield a new GUID per session.

One could perhaps use the MAC address of the machine - but then you’d get the same ID if multiple users use the same machine.

It would be nice if the API was able to provide some interaction with the logged in user in SU. However, even that has it’s limitations - as SU doesn’t require the user to be logged in at all time. So for the purpose of the OP it might also not be sufficient.

Maybe, user MAC address in addition to current user path? Then again that ties the ID to a machine.

1 Like

I did not tell the OP to do this each session. I told them to get it once and save it into the registry as his plugin’s user_id from then on. (This above, was for creating some globally unique id string for later persistent id’ing. I thought ya’ read the whole thread?)

I also told them how to get the OS logged in user name via ENV.

Perhaps also they’d prompt the user for some password.

1 Like