Jump to content


p4java Reuse p4ticket from p4tickects file

P4JAVA p4tickect

  • Please log in to reply
9 replies to this topic

#1 chandrabhanu_27

chandrabhanu_27

    Newbie

  • Members
  • Pip
  • 5 posts
  • LocationPune

Posted 16 April 2014 - 11:30 AM

Hi!

I am new to p4java and so would like some help regarding reuse of p4ticket generated during "p4 login -a" across a number of applications.

To be able to connect and fetch data from perforce following code is used

    server = getServer(serverAddress, null);
server.setUserName(userName);
server.login(password);

Everytime a login is performed a new p4 ticket is used for the application which seem to have a self life of around an year as far I was communicated by my perforce admin.

      server. getLoginStatus() ---  did confirm that which essentially performs similar to "p4 login -s"

Now, I have a number of applications logining in to the perforce server using the very same username and password.
So, everytime one application is up it performs login and invalidates the perforce ticket used by another leading to it to crash. All the applications run independent of one another and have to use the same machine.

                   So, in p4java is there a way of using the same p4 ticket across a number of applications?
As, the ticket are stored in %USERPROFILE%/p4tickets.txt and /home/<user>/.p4tickets in windows and linux respectively,

so, Can I fetch the ticket directly from p4tickets file and so don't have to perform login everytime? This will also avoid the need to hard code the password into the application, an added feature.

When the ticket expires a manual "p4 login -a" can be performed to renew the tickets.

What I am looking for is equivalent api call for "p4 tickets"?

#2 P4Matt

P4Matt

    Advanced Member

  • Members
  • PipPipPip
  • 1383 posts

Posted 17 April 2014 - 06:09 AM

You shouldn't have to perform the login yourself if you have a valid ticket; p4java should see the ticket file automatically and use it. Have you tried logging in manually and then just pulling the login call out of your code?

#3 chandrabhanu_27

chandrabhanu_27

    Newbie

  • Members
  • Pip
  • 5 posts
  • LocationPune

Posted 17 April 2014 - 09:47 AM

Do you mean just do

server = getServer(serverAddress, null);
server.setUserName(userName);

and omit the

server.login(password);

Yes, I did try that. But it complained of not having logged in and showed that P4PASSWD is not set.
I had also called
server.getAuthTicket()             after server.setUserName();
but it returned NULL.
However, on terminal when I typed "p4 -u <username> login -s" it did show that the p4tickects are valid.
The environmental variables as P4PORT, P4USER and P4TICKETS are set. Do I need to set any other variables. Will deleting the .p4tickects file and then manually loggining in "p4 login -a" help in anyway.

#4 chandrabhanu_27

chandrabhanu_27

    Newbie

  • Members
  • Pip
  • 5 posts
  • LocationPune

Posted 17 April 2014 - 12:08 PM

Hi!

I noticed one more thing.

If I set the generated P4TICKET (in hash form EA982829........) during "p4 login -a" manually into

server.setAuthTickect('EA982829.....");

then I need not call server.login(password) and every thing works.

But how do I retrieve the P4TICKET from p4tickects file. Is there a p4java api for it or will I have to write my own functions for it?
Also, please note the p4tickets file does contain tickets across various users and different P4PORTs.

Still wondering why the P4JAVA api failed to pick up the required ticket from p4tickets file on its own even after setting P4TICKETS environment and providing the "severAdress" in getServer().

#5 P4Matt

P4Matt

    Advanced Member

  • Members
  • PipPipPip
  • 1383 posts

Posted 17 April 2014 - 06:24 PM

I'm not sure why it would fail to see the tickets file. Is the application that is using p4java running as a different system user?

I thought they had added a method to get the tickets file; let me see if I can find it. Otherwise you'll need to write a method to get the file.

#6 P4Matt

P4Matt

    Advanced Member

  • Members
  • PipPipPip
  • 1383 posts

Posted 17 April 2014 - 07:48 PM

It appears there are a number of methods for interacting with the ticket and ticket file:

http://www.perforce....ava.lang.String)

I'd check that getTicketsFilePath() returns the path that you expect and see if getAuthTicket() returns the value that you expect.

#7 chandrabhanu_27

chandrabhanu_27

    Newbie

  • Members
  • Pip
  • 5 posts
  • LocationPune

Posted 18 April 2014 - 04:23 PM

Thanks! your pointers really helped me a lot.
Now all I have to do is this

        server = ServerFactory.getOptionsServer(serverAddress, null);
        server.connect();

        // Login
        server.setUserName(userName);
        server.setAuthTicket(userName, server.getAuthTicket(userName));

The new api's you pointed to did the job.

But I have just another doubt.

After my code calls setAuthTicket() and then I type "p4 -u userName login -s" on terminal even though the tickect value remains the same I noticed sometimes the expiration period or life of ticket increases. I also saw the above when calling getLoginStatus() from code.
How does this happen? Does setAuthTicket() renew the ticket period though it remains the same? Can it cause failures for other applications using the same authentication ticket?

#8 P4Matt

P4Matt

    Advanced Member

  • Members
  • PipPipPip
  • 1383 posts

Posted 18 April 2014 - 06:54 PM

If you have a valid ticket and you run 'p4 login' again we bump up the time by a couple hours. We'll do this up to a maximum of half your maximum ticket time out. We had a number of people who work too many hours who hated getting logged out and wanted to bump up their ticket times. =) It won't break anything.

#9 chandrabhanu_27

chandrabhanu_27

    Newbie

  • Members
  • Pip
  • 5 posts
  • LocationPune

Posted 21 April 2014 - 12:46 PM

Is calling
server.setAuthTicket(userName, server.getAuthTicket(userName));
same as calling
server.login()  [or p4 login / p4 login -a on terminal]

I suppose no. Please do confirm!
server.login() causes old ticket to expire and creates a new ticket at times, which I want to avoid as it will cause other running applications to fail.

What does server.setAuthTicket(userName, server.getAuthTicket(userName)) exactly do?
Is it same as calling "p4 -P <tickect value>" (got it from p4 help login)? My intention here is to just keep using the same ticket until it expires rather than performing server.login() or "p4 login" again and again.

>> We'll do this up to a maximum of half your maximum ticket time out.
Sorry, I could not follow. Do you mean the ticket time can be extended when called for half of ticket timeout times?

It was quite interesting to find out that ticket timeout can be bumped up. "p4 login" wiki http://www.perforce....dref/login.html confirms it which I had never noticed earlier.
Just wanted to ask for how many times it is allowed or can it be bumped up an unlimited number of times until the timeout expires?
Then how does p4 login / p4 login -a creates a new ticket while the older ticket is still valid?
Is there any other way of just asking to bump up the ticket timeout period other than p4 login to avoid reissue of p4 ticket?

#10 P4Matt

P4Matt

    Advanced Member

  • Members
  • PipPipPip
  • 1383 posts

Posted 21 April 2014 - 07:37 PM

View Postchandrabhanu_27, on 21 April 2014 - 12:46 PM, said:

Is calling
server.setAuthTicket(userName, server.getAuthTicket(userName));
same as calling
server.login()  [or p4 login / p4 login -a on terminal]

I suppose no. Please do confirm!

server.login() causes old ticket to expire and creates a new ticket at times, which I want to avoid as it will cause other running applications to fail.

What does server.setAuthTicket(userName, server.getAuthTicket(userName)) exactly do?
Is it same as calling "p4 -P <tickect value>" (got it from p4 help login)? My intention here is to just keep using the same ticket until it expires rather than performing server.login() or "p4 login" again and again.
Those two methods manipulate the ticket file for you. Practically speaking you'd probably never call setAuthTicket yourself; you would have to generate a ticket value using 'p4 login -p' and then store it using setAuthTicket(), and at that point you might as well just call 'p4 login' and let the system handle everything for you.

If you want to check the status of your ticket without generating a new one create a LoginOptions object using the string based constructor and pass in "-s" as an argument. Then you can get your ticket status and choose how to react.

Quote

>> We'll do this up to a maximum of half your maximum ticket time out.
Sorry, I could not follow. Do you mean the ticket time can be extended when called for half of ticket timeout times?

It was quite interesting to find out that ticket timeout can be bumped up. "p4 login" wiki http://www.perforce....dref/login.html confirms it which I had never noticed earlier.
Just wanted to ask for how many times it is allowed or can it be bumped up an unlimited number of times until the timeout expires?
Then how does p4 login / p4 login -a creates a new ticket while the older ticket is still valid?
Is there any other way of just asking to bump up the ticket timeout period other than p4 login to avoid reissue of p4 ticket?
Apparently I was wrong; you can only bump it up a maximum of a 1/3 the ticket time. From the docs:

Quote

To extend a ticket's lifespan, use p4 login while already logged in. Your ticket's lifespan is extended by 1/3 of its initial timeout setting, subject to a maximum of your ticket's initial timeout setting.

There is sadly no other way to ask for the ticket time to be bumped up. 'p4 login' does it all.





Also tagged with one or more of these keywords: P4JAVA, p4tickect

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users