Giter Site home page Giter Site logo

UserDelegationKey fails with 500 about azurite HOT 2 OPEN

NaridaL avatar NaridaL commented on August 24, 2024
UserDelegationKey fails with 500

from azurite.

Comments (2)

NaridaL avatar NaridaL commented on August 24, 2024

OK, issue seems to be that the claims "tid" and "oid" were missing from by dummy auth token. These should probably be validated as part of the token validation.

For reference here is a TokenCredential which works:

namespace My.Test.Framework;

using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Microsoft.IdentityModel.Tokens;

public class SelfSignedTokenCredential : TokenCredential
{
    private readonly TimeSpan _expiration = TimeSpan.FromHours(1);
    private readonly string _tenant = "aaaaaaaa-aaaa-aaaa-0000-aaaaaaaaaaaa"; 
    private readonly string _issuer = $"https://sts.windows.net/aaaaaaaa-aaaa-aaaa-0000-aaaaaaaaaaaa/";
    private readonly byte[] _secret = RandomNumberGenerator.GetBytes(32);

    public override async ValueTask<AccessToken> GetTokenAsync(
        TokenRequestContext requestContext,
        CancellationToken cancellationToken)
    {
        return GetToken(requestContext, cancellationToken);
    }

    public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken)
    {
        var audience = requestContext.Scopes.Length > 0 ? requestContext.Scopes[0] : "default-audience";
        audience = audience.Replace("//.default", "/");
        var expires = DateTimeOffset.UtcNow + _expiration;
        var token = GenerateJwtToken(audience, DateTime.UtcNow - TimeSpan.FromMinutes(5), expires.UtcDateTime);
        return new AccessToken(token, expires);
    }

    private string GenerateJwtToken(string audience, DateTime notBefore, DateTime? expires)
    {
        var securityKey = new SymmetricSecurityKey(_secret);
        var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

        return new JwtSecurityTokenHandler().CreateEncodedJwt(
            issuer: _issuer, 
            audience: audience,
            subject: new ClaimsIdentity(new []
            {
                new Claim("oid", "c0ffee00-c0ff-eeee-0000-c0ffee000000"),
                new Claim("tid", _tenant),
            }),
            notBefore: notBefore,
            expires: expires,
            issuedAt: null,
            signingCredentials: credentials);
    }
}

from azurite.

blueww avatar blueww commented on August 24, 2024

@EmmaZhu

Would you please help to look at this issue?

from azurite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.