Skip to content

Commit

Permalink
minecraft/dial.go: Return error in readChainIdentityData instead of p…
Browse files Browse the repository at this point in the history
…anicking (#272)
  • Loading branch information
HashimTheArab authored Dec 16, 2024
1 parent 09b06ae commit 0c8e25b
Showing 1 changed file with 11 additions and 7 deletions.
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("read chain: read json: %w", err)
}
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("read chain: parse jwt: %w", err)
}
if err := tok.UnsafeClaimsWithoutVerification(&claims); err != nil {
panic("invalid chain data from authentication: " + err.Error())
return login.IdentityData{}, fmt.Errorf("read chain: read claims: %w", err)
}
if claims.ExtraData.Identity == "" {
panic("chain data contained no data")
return login.IdentityData{}, fmt.Errorf("read chain: no extra data found")
}
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

0 comments on commit 0c8e25b

Please sign in to comment.