npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xmcl/user

v4.2.0

Published

Minecraft user related functions, including Yggdrasil authenticator, player skin fetcher, and Mojang security API

Downloads

1,621

Readme

User Module

npm version Downloads Install size Downloads Install size npm Build Status

Provide Yggdrasil auth and profile service for Minecraft protocol.

Usage

Microsoft API

Due to the complexity of the Microsoft authentication. The library only provide parts of microsoft login process. You need to implement the rest of the process by yourself.

Overall, according to the wiki.vg, the MS login process can break into following steps:

  1. Aquire Microsoft access token by oauth (user -> ms token)
  2. Acquire XBox token by Microsoft access token (ms token -> xbox token)
  3. Acquire Minecraft access token by XBox token (xbox token -> minecraft token)

The library does not cover the first step.

  • For nodejs, you can use msal-node to implement 1st step.
  • For browser, you can use msal-browser to implement 1st step.

If you want a reference, this is the live example for nodejs/electron using msal-node.

Here we only demo the case you already got the Microsoft access token.

import { MicrosoftAuthenticator } from '@xmcl/user'

const authenticator = new MicrosoftAuthenticator();

const msAccessToken: string; // the access token you got from msal

const { liveXstsResponse, minecraftXstsResponse } = await authenticator.acquireXBoxToken(msAccessToken);

// You can use liveXstsResponse to get the xbox user avatar and name.
const const xboxGameProfile = await authenticator.getXboxGameProfile(xstsResponse.DisplayClaims.xui[0].xid, xstsResponse.DisplayClaims.xui[0].uhs, liveXstsResponse.Token);

// you can use the xstsResponse to get the minecraft access token
const mcResponse = await authenticator.loginMinecraftWithXBox(minecraftXstsResponse.DisplayClaims.xui[0].uhs, minecraftXstsResponse.Token);

// the accessToken is the common minecraft token we want!
const accessToken: string = mcResponse.access_token;
const username = mcResponse.username;
const expire = mcResponse.expires_in; // in seconds

Yggdrasil API

Most of third-party authentication service is based on Yggdrasil API. See authlib-injector for more information.

The legacy mojang auth server, https://authserver.mojang.com is also a yggdrasil api server, but it is not recommended to use it.

import { YggdrasilClient, YggrasilAuthentication } from "@xmcl/user";

const client = new YggdrasilClient('http://random.authserver');
const username: string;
const password: string;
const clientToken: string; // you can use uuid to generate a client token
// login
const auth: YggrasilAuthentication = await client.login({ username, password, clientToken });

// validate access token, you can use this when you restart the program
const valid: boolean = await client.validate(auth.accessToken, clientToken);

// refresh access token, if token is invalid, you can use this to get a new one
const newAuth: YggrasilAuthentication = await client.refresh({ accessToken: auth.accessToken, clientToken });

Third-party Yggdrasil API

The authlib-injector also implements several API for user skin operation.

We also support these API:

import { YggdrasilThirdPartyClient } from "@xmcl/user";

const client = new YggdrasilThirdPartyClient('http://random.authserver');

const uuid: string; // user uuid

// lookup user profile with texture
const profile = await client.lookup(uuid);
const infos: GameProfile.TexturesInfo | undefined = getTextures(gameProfile);
const skin: GameProfile.Texture = infos!.textures.SKIN!;
const skinUrl: string = skin.url; // use url to display skin
const isSlim: boolean = GameProfile.Texture.isSlim(skin); // determine if model is slim or not

// set user skin
const accessToken: string;
const skinUrl: string;
await client.setTexture({ accessToken, uuid, type: "skin", texture: { url: skinUrl } });

// set user skin via binary
const skinData: Uint8Array;
await client.setTexture({ accessToken, uuid, type: "skin", texture: { data: skinData } });

Offline

import { offline } from "@xmcl/user";

// create a offline user
const offlineUser = offline("username");

// create an offline user with uuid
const offlineUser1 = offline("username", "uuid");