title: AWS Cognito Sign-In with Java SDK for desktop application
body: <p>I searched a lot, but it seems impossible to find a solution from start to finish to this topic. As a premise, I've already implemented Cognito sign-up, sign-in and refresh of credentials in two native apps for both iOS and Android, so I have developed a (at least) basic understanding of the authentication flow.</p>
  
  <p>These mobile apps use the simplest cognito setup possible: a user pool, an identity pool with a IAM role for authenticated users and no unauthenticated usage possible. I'm not using (at least for now) Facebook, Google or Amazon login, nor other authentication methods.</p>
  
  <p>Now I need to make a desktop version of those apps in Java, and it seems to me a completely different beast. What I would like to do is this:</p>
  
  <ol>
  <li>Open the login window in my Java desktop application;</li>
  <li>Insert username and password in their fields and press a login button;</li>
  <li>Getting some credentials and start using the application connecting to other AWS services, specifically I need to use S3, Lambda and DynamoDB.</li>
  </ol>
  
  <p>The way to achieve this is on paper reasonably simple:</p>
  
  <ol>
  <li>Get a token from a Cognito user pool;</li>
  <li>Give this token to a Cognito identity pool in exchange for some credentials;</li>
  <li>Use this credentials to access other AWS services.</li>
  </ol>
  
  <hr>
  
  <p>After reading a lot of documentation, downloading a lot of different projects examples and a lot of despair, I've eventually found the way to implement this in the mobile apps. For example, in Android the authentication flow works like this:</p>
  
  <ol>
  <li>Instantiate a CognitoUserPool, using a UserPoolID, an AppClientID, a PoolRegion and (optionally) a ClientSecret;</li>
  <li>Instantiate a credential provider, using an IdentityPoolID and a PoolRegion;</li>
  <li>In the app UI, insert a username and password, and press the Login button;</li>
  <li>Retrieve a CognitoUser using that username from the UserPool instantiated earlier;</li>
  <li>Get a CognitoUserSession for that CognitoUser, using an AuthenticationHandler with various callbacks to pass the password when needed;</li>
  <li>Add that CognitoUserSession to the credentials provider instantiated earlier, in form of a TokenKey + the JWT token extracted from the session.</li>
  <li>At this point, whenever I need to access S3, Lambda or DynamoDB, I simply pass this credentials provider as a parameter for their clients constructors.</li>
  </ol>
  
  <p>To implement the same functionality with the Java SDK seems to me much more difficult. </p>
  
  <p>I managed to implement users Sign-Up fairly easily. However with users Sign-In I don't know where to start at all. </p>
  
  <p>Every example does this in a different way. On top of that, every example uses particular use cases such as developer authenticated Sign-Ins, or custom urls to connect to some owned backend. Why is so difficult to find an example for a basic use case like the one I need? I'm starting to think my basic use case is not basic at all, but rather atypical. Why would login with a username and a password against the default users/credentials service for AWS be atypical, however, I don't really know.</p>
  
  <p>The best I've done so far is copying the relevant classes <a href="https://github.com/aws-samples/aws-cognito-java-desktop-app" rel="noreferrer">from this example project</a> (from which I've also taken the Sign-Up part, that works pretty well) and getting to print the IdToken, AccessToken and RefreshToken in the console. They are printed correctly and are not null.
  What I cant't really understand is how to get the credentials and add them to a credentials provider in order to instantiate the clients to access other AWS services. The only way I see in the project to do that is to call the method</p>
  
  <p><code>Credentials getCredentials(String accessCode)</code></p>
  
  <p>which I suppose it should accept the access code retrieved with the InitAuth method (that starts an OAuth2.0 authentication flow, please correct me if I am wrong). The problem is that I can't find a way to retrieve that code. I can't find an online example of an access code to see how it looks. I tried to put one of the tokens and the web request responds</p>
  
  <pre><code>{"error":"invalid_grant"}
  </code></pre>
  
  <p>which suggests its not a valid code, but at least the web request is valid.</p>
  
  <p>To make it more clear, what I can do is this:</p>
  
  <pre><code>String username; //retrieved from UI
  String password; //retrieved from UI
  
  //I copied AuthenticationHelper as is from the project
  AuthenticationHelper helper = new AuthenticationHelper(POOL_ID, CLIENT_APP_ID, CLIENT_SECRET);
  
  //I then retrieve the tokens with SRP authentication
  AuthenticationResultType result = helper.performSRPAuthentication(username, password);
  
  //Now I can successfully print the tokens, for example:
  System.out.println(result.getAccessToken());
  </code></pre>
  
  <p><strong>How can I retrieve the credentials from here? Where I should put the identity pool id?</strong> In Android I simply add the JWT token to an <code>HashMap</code> and use it like </p>
  
  <p><code>credentialsProvider.setLogins(loginsMap)</code>.</p>
  
  <p>Furthermore, this project contain classes with hundreds of lines of code, BigInteger variables, hardcoded strings of many lines of random characters (some sort of key or token I suppose) and other black magic like that (especially in the AuthenticationHelper class). Another thing I don't like about this solution is that it retrieves credentials through manually written web requests (with another separated class created ad hoc to make the request). Really isn't there in the Java SDK some handy method that wraps all those things in a bunch of elegant lines of code? Why call it an SDK than? The iOS and Android SDKs handle all that on their own in such a simpler way. Is this due to the fact that they expect a developer of desktop app to be way more able/expert, in contrast to the average guy that some day, getting up from bed, decides to make an iOS/Android app <em>[alludes to himself]</em>? This would explain their effort to make the mobile SDKs so developer-friendly in comparison.</p>
  
  <p>Onestly I find really hard to believe that I have to do that, reading who knows what on a doc page who knows where, to Sign-In a user, which makes me think that I'm really missing something. I literally read every stack exchange question and documentation I was able to find. The fact is that there is almost always an AWS documentation page for what I need, but to actually find it is not so simple sometimes, at least for Cognito documentation.</p>
  
  <p>I read that I can put a file with the needed credentials in the PC filesystem and the Java SDK will use those credentials to access all the resources, however from my understanding this method is reserved to Java applications running on a server as a backend (servlets), where the end user can't access them through his browser. My application is a desktop app for end-users, so I can't even consider to leave AWS credentials on the user PC (please correct me if I'm wrong, I would really love to make something so simple).</p>
  
  <hr>
  
  <p>What really scares me is that Sign-In with a user pool and identity pool might not be possible at all. I know that Cognito related stuff was added to the Java SDK much later it was available for iOS, Android and JavaScript. But if they added it, I suppose It should support an authentication flow at least similar to those of the mobile counterparts.</p>
  
  <p>What worsen the problem even more is that I initially made all the functionalities of my application to work offline. I thought that I would have eventually integrated AWS in the app. In this way the application is a bit more modular, and the AWS related stuff in concentrated in a package, detatched from the rest of the application logic and UI. In my sea of ignorance this seems a good practice to me, but now, if I cannot manage to resolve this problem, I've thrown months of work in the trash, only to realize that I have to build a web app beacuse JavaScript is much more supported.</p>
  
  <p>Even on MobileHub the only options to create a ClientApp on Cognito is for iOS, Android, JavaScript and React-something.
  When documentation or examples are provided for other languages/SDKs, Java is often omitted and the most frequent I see among the options is .NET.
  To make my frustration even bigger, everytime I search something on a search engine, the fact that the word "Java" is contained in the word "JavaScript" obfuscates the few results that could be useful, because all the JavaScript SDK related stuff is generally higher ranked in the search engines than Java's (this could explain in part why .NET related stuff seems more easy to find, at least on StackOverflow or others Q&amp;A sites).</p>
  
  <hr>
  
  <p>To conclude, all of this created some questions in my head:</p>
  
  <ol>
  <li><p><strong>Why so few people seem to need this authentication method (with username and password)?</strong> It seems to me a pretty common and reasonable use case for a desktop application. I know that web apps growth is through the roof, but given that Java is one of the most used languages today, how is it possible that nobody needs to do a simple login from a desktop application? Which leads to the next question:</p></li>
  <li><p><strong>Is there something inherently bad/wrong/risky/stupid in using the Java SDK for a desktop application?</strong> Is it intended only for usage on a server as backend or for a web app? What should be the solution than, to make a desktop application that connects to AWS services? It is wrong to do an AWS connected desktop app at all? Should a web app the only option to consider? Why? I opted for Java to implement an application that would run on Widows, macOS and Linux. I also chose Java because i thought it would be mostly similar to the Android SDK in its usage, given its code should be indipendent from the platform UI, making simple to reuse code. I was wrong.</p></li>
  <li><p>If there's nothing wrong in using the Java SDK like this, could some
  good soul please help me find an example that goes from putting a
  username and a password in two fields, and instantiate a client to
  access other AWS services (such as an S3 client) in a Java desktop
  application?</p></li>
  </ol>
  
  <p>Tell me everything you need to know and I'll edit the question.</p>
  
  <p>Please someone help me, I'm loosing my mind.</p>