Ejemplo n.º 1
0
        public static string CreateToken(List <Claim> claims)
        {
            Chilkat.Jwt jwt = new Chilkat.Jwt();

            //  Build the JOSE header
            Chilkat.JsonObject jose = new Chilkat.JsonObject();
            //  Use HS256.  Pass the string "HS384" or "HS512" to use a different algorithm.
            bool success = jose.AppendString("alg", "HS256");

            jose.AppendString("typ", "JWT");

            //  Now build the JWT claims (also known as the payload)
            Chilkat.JsonObject jsonClaims = new Chilkat.JsonObject();
            //success = jsonClaims.AppendString("issuer", "http://www.vouchers.net");
            //success = jsonClaims.AppendString("aud", "http://www.vouchers.net");

            //  Set the timestamp of when the JWT was created to now.
            int curDateTime = jwt.GenNumericDate(0);

            jsonClaims.AddIntAt(-1, "iat", curDateTime);

            //  Set the "not process before" timestamp to now.
            jsonClaims.AddIntAt(-1, "nbf", curDateTime);

            //  Set the timestamp defining an expiration time (end time) for the token
            //  to be now + 1 hour (3600 seconds)
            jsonClaims.AddIntAt(-1, "exp", curDateTime + 3600);

            //  Produce the smallest possible JWT:
            jwt.AutoCompact = true;

            string strJwt = jwt.CreateJwt(jose.Emit(), jsonClaims.Emit(), "Pa$$w0rd");

            return(strJwt);
        }
Ejemplo n.º 2
0
        // URLs to help understand this topic:
        // https://developers.google.com/identity/protocols/OAuth2InstalledApp
        //
        private bool oauth2_google(string scope)
        {
            // Generates state and PKCE values.
            string state         = m_prng.GenRandom(32, base64Url);
            string code_verifier = m_prng.GenRandom(32, base64Url);

            Chilkat.Crypt2 crypt = new Chilkat.Crypt2();
            crypt.EncodingMode  = base64Url;
            crypt.HashAlgorithm = "SHA256";
            string       code_challenge        = crypt.HashStringENC(code_verifier);
            const string code_challenge_method = "S256";

            //Create a Chilkat socket for listening.  Begin listening asynchronously.
            Chilkat.Socket listenSocket = new Chilkat.Socket();
            int            backlog      = 5;
            int            listenPort   = 0;

            // Passing a listenPort = 0 causes BindAndListen to find a random unused port.
            // The chosen port will be available via the ListenPort property.
            if (!listenSocket.BindAndListen(listenPort, backlog))
            {
                fgAppendToErrorLog(listenSocket.LastErrorText);
                popupError("Failed to BindAndListen");
                return(false);
            }

            // Get the chosen listen port
            // This ListenPort property is available starting in Chilkat v9.5.0.59
            listenPort = listenSocket.ListenPort;

            // Creates a redirect URI using an available port on the loopback address.
            string redirect_uri = "http://127.0.0.1:" + listenPort.ToString() + "/";

            //Wait a max of 5 minutes.  The OnTaskCompleted event is called when an incoming connection
            //arrives, or when the listen failed.
            listenSocket.OnTaskCompleted += listenSocket_OnTaskCompleted;
            Chilkat.Task task = listenSocket.AcceptNextConnectionAsync(5 * 60000);
            if (task == null)
            {
                MessageBox.Show("Failed to start socket accept...");
                return(false);
            }

            // Add some information that will be needed by the TaskCompleted event..
            Chilkat.JsonObject taskData = new Chilkat.JsonObject();
            taskData.AppendString("code_verifier", code_verifier);
            taskData.AppendString("redirect_uri", redirect_uri);
            task.UserData = taskData.Emit();

            // Start the task.
            task.Run();

            // Creates the OAuth 2.0 authorization request.
            Chilkat.StringBuilder sbAuthRequest = new Chilkat.StringBuilder();
            sbAuthRequest.Append(authorizationEndpoint);
            sbAuthRequest.Append("?response_type=code&scope=");
            sbAuthRequest.Append(m_encoder.EncodeString(scope, "utf-8", "url"));
            sbAuthRequest.Append("&redirect_uri=");
            sbAuthRequest.Append(m_encoder.EncodeString(redirect_uri, "utf-8", "url"));
            sbAuthRequest.Append("&client_id=");
            sbAuthRequest.Append(googleAppClientId);
            sbAuthRequest.Append("&state=");
            sbAuthRequest.Append(state);
            sbAuthRequest.Append("&code_challenge=");
            sbAuthRequest.Append(code_challenge);
            sbAuthRequest.Append("&code_challenge_method=");
            sbAuthRequest.Append(code_challenge_method);

            // Here is a shorter way of building the URL in C#
            //string authorizationRequest = string.Format("{0}?response_type=code&scope={6}&redirect_uri={1}&client_id={2}&state={3}&code_challenge={4}&code_challenge_method={5}",
            //    authorizationEndpoint,  // 0
            //    System.Uri.EscapeDataString(redirect_uri), // 1
            //    googleAppClientId, // 2
            //    state, // 3
            //    code_challenge, // 4
            //    code_challenge_method, // 5
            //    System.Uri.EscapeDataString(scope)); // 6

            // Get authorization from Google account owner...
            webBrowser1.Navigate(sbAuthRequest.GetAsString());

            return(true);
        }