Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return error in readChainIdentityData instead of panicing #272

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions minecraft/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ func (d Dialer) DialContext(ctx context.Context, network, address string) (conn
if err != nil {
return nil, &net.OpError{Op: "dial", Net: "minecraft", Err: err}
}
d.IdentityData = readChainIdentityData([]byte(chainData))
identityData, err := readChainIdentityData([]byte(chainData))
if err != nil {
return nil, &net.OpError{Op: "dial", Net: "minecraft", Err: err}
}
d.IdentityData = identityData
}

n, ok := networkByID(network, d.ErrorLog)
Expand Down Expand Up @@ -256,26 +260,26 @@ func (d Dialer) DialContext(ctx context.Context, network, address string) (conn

// readChainIdentityData reads a login.IdentityData from the Mojang chain
// obtained through authentication.
func readChainIdentityData(chainData []byte) login.IdentityData {
func readChainIdentityData(chainData []byte) (login.IdentityData, error) {
chain := struct{ Chain []string }{}
if err := json.Unmarshal(chainData, &chain); err != nil {
panic("invalid chain data from authentication: " + err.Error())
return login.IdentityData{}, fmt.Errorf("invalid chain data from authentication: %w", err)
HashimTheArab marked this conversation as resolved.
Show resolved Hide resolved
}
data := chain.Chain[1]
claims := struct {
ExtraData login.IdentityData `json:"extraData"`
}{}
tok, err := jwt.ParseSigned(data)
if err != nil {
panic("invalid chain data from authentication: " + err.Error())
return login.IdentityData{}, fmt.Errorf("invalid chain data from authentication: %w", err)
HashimTheArab marked this conversation as resolved.
Show resolved Hide resolved
}
if err := tok.UnsafeClaimsWithoutVerification(&claims); err != nil {
panic("invalid chain data from authentication: " + err.Error())
return login.IdentityData{}, fmt.Errorf("invalid chain data from authentication: %w", err)
HashimTheArab marked this conversation as resolved.
Show resolved Hide resolved
}
if claims.ExtraData.Identity == "" {
panic("chain data contained no data")
return login.IdentityData{}, fmt.Errorf("chain data contained no data")
HashimTheArab marked this conversation as resolved.
Show resolved Hide resolved
}
return claims.ExtraData
return claims.ExtraData, nil
}

// listenConn listens on the connection until it is closed on another goroutine. The channel passed will
Expand Down