Skip to content

Add last.fm provider #12903

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/2_bug_provider.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ body:
- "Frontegg"
- "Keycloak"
- "Kinde"
- "Last.fm"
- "Line"
- "LinkedIn"
- "Logto"
Expand Down
4 changes: 4 additions & 0 deletions docs/public/img/providers/lastfm.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
189 changes: 189 additions & 0 deletions packages/core/src/providers/lastfm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/**
* <div class="provider" style={{backgroundColor: "#000", display: "flex", justifyContent: "space-between", color: "#fff", padding: 16}}>
* <span>Built-in <b>Last.fm</b> integration.</span>
* <a href="https://www.last.fm/">
* <img style={{display: "block"}} src="https://authjs.dev/img/providers/lastfm.svg" height="48" />
* </a>
* </div>
*
* @module providers/lastfm
*/
import { createHash } from "crypto";

import type { TokenSet } from "../types.js";
import type { OAuthConfig, OAuthUserConfig } from "./index.js";

export interface LastfmSignatureParams {
method: string;
token: string;
api_key: string;
api_secret: string;
}

export interface LastfmSession {
session: {
name: string;
key: string;
subscriber: string;
};
}

export interface LastfmProfileImage {
size: "small" | "medium" | "large" | "extralarge";
"#text": string;
}

export interface LastfmProfile extends Record<string, any> {
user: {
name: string;
age: string;
subscriber: string;
realname: string;
bootstrap: string;
playcount: string;
artist_count: string;
playlists: string;
track_count: string;
album_count: string;
image: LastfmProfileImage[];
registered: {
unixtime: string;
"#text": number;
};
country: string;
gender: string;
url: string;
type: string;
};
}

function generateApiSig({
method,
token,
api_key,
api_secret,
}: LastfmSignatureParams): string {
const raw = `api_key${api_key}method${method}token${token}${api_secret}`;

return createHash("md5").update(raw).digest("hex");
}

/**
* Add Last.fm login to your page.
*
* ### Setup
*
* #### Callback URL
* ```
* https://example.com/api/auth/callback/lastfm
* ```
*
* #### Configuration
*```ts
* import { Auth } from "@auth/core"
* import Lastfm from "@auth/core/providers/lastfm"
*
* const request = new Request(origin)
* const response = await Auth(request, {
* providers: [
* Lastfm({
* clientId: LASTFM_API_KEY,
* clientSecret: LASTFM_SHARED_SECRET,
* }),
* ],
* })
* ```
*
* ### Resources
*
* - [Last.fm OAuth documentation](https://www.last.fm/api/webauth)
* - [Last.fm app console](https://www.last.fm/api/accounts)
*
* ### Notes
*
* By default, Auth.js assumes that the Last.fm provider is
* based on the [OAuth 2](https://www.rfc-editor.org/rfc/rfc6749.html) specification.
*
* :::tip
*
* The Last.fm provider comes with a [default configuration](https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/providers/lastfm.ts).
* To override the defaults for your use case, check out [customizing a built-in OAuth provider](https://authjs.dev/guides/configuring-oauth-providers).
*
* :::
*
* :::info **Disclaimer**
*
* If you think you found a bug in the default configuration, you can [open an issue](https://authjs.dev/new/provider-issue).
*
* Auth.js strictly adheres to the specification and it cannot take responsibility for any deviation from
* the spec by the provider. You can open an issue, but if the problem is non-compliance with the spec,
* we might not pursue a resolution. You can ask for more help in [Discussions](https://authjs.dev/new/github-discussions).
*
* :::
*/
export default function Lastfm<P extends LastfmSession>(
options: OAuthUserConfig<P>
): OAuthConfig<P> {
return {
id: "lastfm",
name: "Last.fm",
type: "oauth",
authorization: {
url: "http://www.last.fm/api/auth",
params: { api_key: options.clientId },
},
token: {
request: async ({ provider, params }) => {
const token = params.token;

const apiSig = generateApiSig({
method: "auth.getSession",
token,
api_key: provider.clientId!,
api_secret: provider.clientSecret!,
});

const res = await fetch(
`https://ws.audioscrobbler.com/2.0/?method=auth.getSession&api_key=${provider.clientId}&token=${token}&api_sig=${apiSig}&format=json`
);

const data = (await res.json()) as LastfmSession;

return {
tokens: {
access_token: data.session.key,
token_type: "bearer",
scope: "",
expires_at: null,
} satisfies TokenSet,
};
},
},
userinfo: {
request: async ({ provider, tokens }) => {
const res = await fetch(
`https://ws.audioscrobbler.com/2.0/?method=user.getInfo&api_key=${provider.clientId}&sk=${tokens.access_token}&format=json`
);

const user = (await res.json()) as LastfmProfile;

return {
session: {
name: user.name,
key: tokens.access_token,
subscriber: user.subscriber,
},
};
},
},
profile(profile) {
return {
id: profile.session.key,
name: profile.session.name,
email: null,
image: null,
};
},
options,
};
}